Jump to content

Sorry if this is in the wrong section or if I'm not posting right but I need some help QUICK.

How come this is outputting "[Function]" instead of what I want the console to print? 

 

Here's the code and the website that I've been using to work on it is http://repl.it
 

var userChoice = prompt("Do you choose rock, paper or scissors?");var computerChoice = Math.random();if (computerChoice < 0.34) {  computerChoice = "rock";} else if (computerChoice <= 0.67) {  computerChoice = "paper";} else {  computerChoice = "scissors";}console.log("Computer: " + computerChoice);var compare = function (choice1, choice2) {  if (choice1 === choice2) {    return console.log("The result is a tie!");  } else if (choice1 === "paper") {    if (choice2 === "rock") {      return console.log("paper wins");    } else {      return console.log("scissors wins");    }  } else {    return console.log("paper wins");  }};var compare = function (userChoice, computerChoice) {  if (choice1 === "scissors") {    if (choice2 === "rock") {      return console.log("rock wins");    } else {      return console.log("scissors wins");    }  }};console.log(compare);

CPU: i7 2600k @4.2GHz GPU: GTX 660 Power Supply: EVGA SupernovaNEX 750W 80+ Bronze RAM: 32GB G. Skill Ripjaws X

Link to comment
https://linustechtips.com/topic/497147-javascript-problem/
Share on other sites

Link to post
Share on other sites

 

Sorry if this is in the wrong section or if I'm not posting right but I need some help QUICK.

How come this is outputting "[Function]" instead of what I want the console to print? 

 

Here's the code and the website that I've been using to work on it is http://repl.it

 

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
  computerChoice = "rock";
} else if (computerChoice <= 0.67) {
  computerChoice = "paper";
} else {
  computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);
 
var compare = function (choice1, choice2) {
  if (choice1 === choice2) {
    return console.log("The result is a tie!");
  } else if (choice1 === "paper") {
    if (choice2 === "rock") {
      return console.log("paper wins");
    } else {
      return console.log("scissors wins");
    }
  } else {
    return console.log("paper wins");
  }
};
 
var compare = function (userChoice, computerChoice) {
  if (choice1 === "scissors") {
    if (choice2 === "rock") {
      return console.log("rock wins");
    } else {
      return console.log("scissors wins");
    }
  }
};
console.log(compare);

 

 

Because you are logging a function:

console.log(compare);

Also please wrap your code in code tags

Link to comment
https://linustechtips.com/topic/497147-javascript-problem/#findComment-6646445
Share on other sites

Link to post
Share on other sites

Because you are logging a function:

console.log(compare);

Also please wrap your code in code tags

Sorry it's my first time posting here. I get what you mean by logging a function but I haven't found any solutions that make sense to me...

CPU: i7 2600k @4.2GHz GPU: GTX 660 Power Supply: EVGA SupernovaNEX 750W 80+ Bronze RAM: 32GB G. Skill Ripjaws X

Link to comment
https://linustechtips.com/topic/497147-javascript-problem/#findComment-6646455
Share on other sites

Link to post
Share on other sites

Sorry it's my first time posting here. I get what you mean by logging a function but I haven't found any solutions that make sense to me...

 

Right so you have this piece of code:

var compare = function (userChoice, computerChoice) {  if (choice1 === "scissors") {    if (choice2 === "rock") {      return console.log("rock wins");    } else {      return console.log("scissors wins");    }  }};console.log(compare); 

Here you are defining compare as a function.

 

Then you are using console.log to log the variable compare to the console. This will cause the function to be logged as it is stored.

Link to comment
https://linustechtips.com/topic/497147-javascript-problem/#findComment-6646498
Share on other sites

Link to post
Share on other sites

You can't return a console log. When you call console.log it immediately writes the given string to the console and has no return value, so returning it will not do anything. 

 

Also, compare has parameters and in your current call to it you do not pass any to it. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
https://linustechtips.com/topic/497147-javascript-problem/#findComment-6646627
Share on other sites

Link to post
Share on other sites

 

var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random();if (computerChoice < 0.34) {  computerChoice = "rock";} else if (computerChoice <= 0.67) {  computerChoice = "paper";} else {  computerChoice = "scissors";}console.log("Computer: " + computerChoice);var compare = function (choice1, choice2) {  if (choice1 === choice2) {    return "The result is a tie!"; //Return returns objects like strings and integers, console.log does not return any value, so it would return "[Function]"  } else if (choice1 === "paper") {    if (choice2 === "rock") {      return "paper wins";    } else {      return "scissors wins";    }  } else {    return "paper wins";  }};var compare = function (choice1, choice2) { //Changed to choice1 and choice2, which are parameters  if (choice1 === "scissors") {    if (choice2 === "rock") {      return "rock wins";    } else {      return "scissors wins";    }  }};//Try to combine the above two functions, to do this, simply combine this as an elseifconsole.log(compare(userChoice, computerChoice)); //Inside the parentheses, we pass two parameters to be compared, these become choice1 and choice2 in the compare function.

 

http://www.w3schools.com/jsref/jsref_return.asp

 

Try the JavaScript course on codecademy.com, it goes over all of this and describes it really well.

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
https://linustechtips.com/topic/497147-javascript-problem/#findComment-6650968
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×