What am I doing wrong


#1

I have been trying to figure this out for a while. What is wrong with this code?

else if (choice2 === "paper") {
if (choice1 === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
}


#2

Please, post all your code, also print error message and link to this lesson.


#3

/*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!";
}
if(choice1 === choice2) {
return "The result is a tie";
}

else if(choice1 === "rock") {

if(choice2 === "scissors") {
    return "rock wins";
}

}
else {
return "paper wins";
}
else if (choice2 === "paper") {
if (choice1 === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
}

SyntaxError: Unexpected token else

https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/3?curriculum_id=506324b3a7dffd00020bf661#


#4

First problem is here:

console.log( COMPUTER: " + computerChoice);*/

Should be:

console.log("COMPUTER: " + computerChoice);

Next problem is in your if-else statements in compare() function. Lets rewrite it step by step:

// step 1: create an empty function
var compare = function(choice1, choice2) {

}

// step 2: write if-else-if statement:
var compare = function(choice1, choice2) {
   if(choice1 === choice2) {
        
    } else if(choice1 === "rock") {
                
    } else if(choice1 === "paper") {
        
    }
}
// now we have a skeleton, all we need - to do what instruction says: write 
// an if / else statement. If choice2 === "rock", return "paper wins". Else, 
// return "scissors wins".

// step 3
var compare = function(choice1, choice2) {
   if(choice1 === choice2) {
      return "The result is a tie!";
   } else if(choice1 === "rock") {
      if(choice2 === "scissors") {
         return "rock wins!";
      } else {
         return "paper wins"; 
      }
   } else if(choice1 === "paper") {
       // here try to write code yourself
       // here should be an if / else statement as I wrote in previous 
       // when choice1 === "rock" but in this time for choice1 === "paper":
       // if choice2 === "rock" => "paper wins" 
       // else => return "scissors wins".
   }
}

This should help you.


#5

Thank you that helped alot


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.