What if Choice is Scissors (Part 8) Help


#1

What if Choice 1 is Scissors Part 8:

I keep getting Syntax Error. I checked scope and logic. I'm not sure what is the problem

Here is my code:

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!";
}
else if(choice1 === "paper")
{
if(choice2 === "rock")
{
return "paper wins";
}
else
{
return "scissors wins";
}
}
else if(choice1 === "rock")
{
if(choice2 === "scissors")
{
return "rock wins";
}
else
{
return "paper wins";
}
}
else if(choice1 === "scissors")
{
if(choice2 === "rock")
{
return "rock wins";
}
else
{
return "scissors wins";
}
}
};

compare(userChoice, computerChoice);


#2

It appears I didn't indent when I posted my code, but I did.


#3

@salwins17,

1

2

As you are using the return statement in your compare function
you will have to use

console.log( compare( userChoice,computerChoice) );

to have a displayed result....

3

Please read
http://javascript.crockford.com/code.html <---JavaScript code-convention
( how to write an IF statement )

4

Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use
= http://discuss.codecademy.com/t/using-backticks-to-format-your-code/3697/2
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet


#4

My code was
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (0 < computerChoice < 0.34) {
computerChoice = "rock";
} else if(0.34 < computerChoice < 0.66) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function(userChoice, computerChoice) {
if(userChoice === computerChoice) {
return "The result is a tie!";
}
else if(userChoice === "rock") {
if (computerChoice === "scissors") {
return "Rock wins"
}
else {
return "Paper wins";
}
}
else if(userChoice === "paper") {
if(computerChoice === "rock") {
return "Paper wins";
}
else {
return "Scissors wins"
}
}
else if(userChoice === "scissors") {
if(computerChoice === "rock") {
return "Rock wins"
}
else {
return "Scissors wins"
}
}
};
, and
Oops, try again.
Make sure to call your compare function with userChoice and computerChoice as the arguments!
appeared when I clicked the save and submit button.


#5

Hi you should call your compare function with userChoice and computerChoice like that

compare(userChoice, computerChoice)

Add it after your last else statement


#6

@herardojones,

Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on:

  • the creation of the compare function
  • the truth-table
  • the build of the IF ELSE-IF statement structure as part of the compare FUNCTION-BODY
  • the execution of the compare function (keeping in mind that the return statement is used )