8/9 PLZZ help


#1

Im getting tired

console.log("Computer: " + computerChoice);
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"){
if(choice2 ==="rock"){return "paper wins";}
else{return "scissors wins";}
}
else{
if(choice1==="rock"){return "rock wins";}
else{return "scissors wins";}
}
};
compare(userChoice,computerChoice);


#2

Well.... look at this:

You missed something there. (Brackets)


#3

You got lost in the curly-bracket-{ } Forest

Please read code convention on writing IF ELSE-IF
http://javascript.crockford.com/code.html

PS.
Please re-edit your Post

  • 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.


#4

Yeah your code works

//=================================================================================
//================added this part to test it=======================================


var userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();
var computerChoice = Math.random();
var tie =  true;

if (computerChoice < 0.34)      {computerChoice = "rock";} 
else if(computerChoice <= 0.67) {computerChoice = "paper";} 
else                            {computerChoice = "scissors";} 

//=================================================================================
//=================================================================================




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"){
if(choice2 ==="rock"){return "paper wins";}
else{return "scissors wins";}
}
else{
if(choice1==="rock"){return "rock wins";}
else{return "scissors wins";}
}
};
console.log(compare(userChoice,computerChoice));

generally the if block without the braces will not give an error. Thats because when the number of statements in your if/else block is just 1 then you can forgo the {} but it is generally a good idea to include it all the time