7. Can't seem to figure out what is the matter with my code :/


#1



I understand what it's trying to get me to do but I can't seem to understand where my code is going wrong at.


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

//Step Number Seven Below

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


#2

you build the same thing 3 times, a else if statement to check what choice1 is. Inside the else if you have a if and a else statement to check what choice2 is, and who won. Like so:

else if (condition){
  if(condition){
    // do something
  } else {
    // do something
  }
}

important here is you should close (}) your else if before starting the next one, and don't forgot to close your function at the very end.


#3

Oh okay.. hmm.. let me see if I can fix it. Thank you by the way :slight_smile:


#4

Hmm... It seems to still not work :confused:

Here is my new code:

var compare = function (choice1, choice2) {

 if(choice1 === choice2) { 
     return("The result is a tie!");
     }
     
  else if(choice2 === "scissors" ) {
      return ("rock wins");
  }
  
  else {
        return ("paper wins");
  } 

//Step Number Seven Below

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

#5

else if(choice2 === "scissors" ) {
      return ("rock wins");
  }
  
  else {
        return ("paper wins");
  } 
/* still not closing (}) else if here */
//Step Number Seven Below

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

you still didn't close your else if before moving on to the next else if


#6

So would I need to insert the } here?

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

else {
return ("paper wins");
}
}//<-----Right here

//Step Number Seven Below
`


#7

based on my first answer:

and the comment i put in my second answer, that does seem like suitable place, doesn't it?


#8

Yeah it does but it's still giving me an error message.


#9

woah, i am sleepy. The if statement is missing inside your first else if. Which you should have build in exercise 6


#10

Yeah I have that built but I think I become confused by the instructions that state "Under the existing code...".

So this is what I have from exercise 6:

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";
}
}
};


#11

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";
        }
    }
   // code for exercise 7. Another else if, with nest if and else like i showed you
};

see comment, there you should code. Under the existing code but still inside the function


#12

Thank you so much! Okay all is well now and it's fixed! Thank you so much agian. You really made my day :slight_smile:

Do you have an tips of I can keep my JavaScript looking organized? I think this is an issue for me is that I often loose track of where each bracket belongs and which closing brackets belong to which opening brackets.


#13

indention is important. You can always add comments to remind yourself which closing bracket belongs to which, but indention is your biggest friend


#14

Okay I will take note of that thank you :slight_smile:


sntaxError: Unexpected token else