Help on exercise 8 please


#1

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

I am stuck for a very long time now on this exercise and i do not know what to do.The error message is “there is something wrong with your syntax”

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




#2

okay, so we have our function with if statement which checks for a tie:

var compare = function(choice1, choice2) {
// indent? 
if(choice1===choice2) {
    return "The result is a tie!";
}

};

so then we are going to add 3 else if statements (i will only demonstrate one) to see what the user (choice1) picked, the demonstration contains pseudo code, not actual code:

var compare = function(choice1, choice2) {
if(choice1===choice2) {
    return "The result is a tie!";
}
     else if (user choices rock){

     }
};

good, now we are going to add if and else inside else if to determine who wins:

var compare = function(choice1, choice2) {
if(choice1===choice2) {
    return "The result is a tie!";
}
     else if (choice1 chose  rock){
          if (choice2 chose paper){
                   return paper/computer wins
          } else {
                   return  rock/user wins
          }
     } 
};

See how i nested both if and else inside else if and how this differs from your code?

then after the else if for rock, we need to add two more else if statements for choice1 being paper and choice1 being scissors:

var compare = function(choice1, choice2) {
if(choice1===choice2) {
    return "The result is a tie!";
}
     else if (choice1 chose  rock){
          if (choice2 chose paper){
                   return paper/computer wins
          } else {
                   return  rock/user wins
          } // closes else
     } // closes else if
    // add else if choice1 is paper here
   // add else if choice1 is scissors here
};

see the comments in the code. If you follow this structure, you should be good.

In your code, choice1 is scissors is after/outside the function, which is wrong
the else if choice1 is rock is missing altogether, while you build it earlier. You can always revisit that exercise if you want


#3

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