This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Mariano Shaar
almost 11 years

Help, it´s urgent!!!

I get the red x at line 22 saying missign operand, and when I run the code, the console says SyntaxError: Unexpected token else

var userChoice = prompt (“Do you choose rock, paper, scissors, lizard, or spock?”)

var computerChoice = Math.random(); if (computerChoice < 0.21) { computerChoice = “rock” } else if (0.20 < computerChoice < 0.41) { computerChoice = “paper” } else if (0.40 < computerChoice < 0.61) { computerChoice = “scissors” } else if (0.60 < computerChoice < 0.81) { computerChoice = “lizard” } else { computerChoice = “spock”}

console.log(“The computer chose” + “ “ + computerChoice);

var compare = function(choice1,choice2) { if (choice1 = choice2) { return “The result is a tie!”} else if (choice1 = “rock”) { if (choice2 = “paper”) {return “Paper wins!” } else if (choice2 = “scissors”) { return “Rock wins!” } else if (choice2 = “lizard”) { return “Rock wins!” } else { return “Spock wins!”} else if (choice1 = “paper”) { if (choice2 = “rock”) {return “Paper wins!” } else if (choice2 = “scissors”) { return “Scissors win!” } else if (choice2 = “lizard”) { return “Lizard wins!” } else { return “Paper wins!”} else if (choice1 = “scissors”){ if (choice2 = “paper”) {return “Scissors wins!” } else if (choice2 = “rock”) { return “Rock wins!” } else if (choice2 = “lizard”) { return “Scissors wins!” } else { return “Spock wins!”} else if (choice1 = “lizard”) { if (choice2 = “paper”) {return “Lizard wins!” } else if (choice2 = “scissors”) { return “Scissors wins!” } else if (choice2 = “rock”) { return “Rock wins!” } else { return “Lizard wins!”} else if (choice1 = “spock”) { if (choice2 = “paper”) {return “Paper wins!” } else if (choice2 = “scissors”) { return “Spock wins!” } else if (choice2 = “lizard”) { return “Lizard wins!” } else { return “Spock wins!”}

};

Answer 5158adc5f5a4bcfa880027ac

0 votes

Permalink

Please put a semicolon after each statement apart from if statements and for comparison we use === not = .Why you want to check computerChoice<0.20 and the method you used for checking is totally wrong. Please refer the underlying code for correction:- var userChoice = prompt (“Do you choose rock, paper, scissors, lizard, or spock?”);

var computerChoice = Math.random(); if (computerChoice < 0.21) { computerChoice = “rock”; } else if ( computerChoice < 0.41) { computerChoice = “paper”; } else if (computerChoice < 0.61) { computerChoice = “scissors”; } else if ( computerChoice < 0.81) { computerChoice = “lizard”; } else { computerChoice = “spock”;}

console.log(“The computer chose” + “ “ + computerChoice);

var compare = function(choice1,choice2) { if (choice1 === choice2) { return “The result is a tie!”;} else if (choice1 === “rock”) { if (choice2 === “paper”) {return “Paper wins!”; } else if (choice2 === “scissors”) { return “Rock wins!”; } else if (choice2 === “lizard”) { return “Rock wins!”; } else { return “Spock wins!”;} else if (choice1 === “paper”) { if (choice2 === “rock”) {return “Paper wins!”; } else if (choice2 === “scissors”) { return “Scissors win!”; } else if (choice2 === “lizard”) { return “Lizard wins!”; } else { return “Paper wins!”;} else if (choice1 === “scissors”){ if (choice2 === “paper”) {return “Scissors wins!”; } else if (choice2 === “rock”) { return “Rock wins!”; } else if (choice2 === “lizard”) { return “Scissors wins!”; } else { return “Spock wins!”;} else if (choice1 === “lizard”) { if (choice2 === “paper”) {return “Lizard wins!”; } else if (choice2 === “scissors”) { return “Scissors wins!”; } else if (choice2 === “rock”) { return “Rock wins!”; } else { return “Lizard wins!”;} else if (choice1 === “spock”) { if (choice2 === “paper”) {return “Paper wins!”; } else if (choice2 === “scissors”) { return “Spock wins!”; } else if (choice2 === “lizard”) { return “Lizard wins!”; } else { return “Spock wins!”;}

};

points
Submitted by Sahla Yoosuf
almost 11 years