This forum is now read-only. Please use our new forums! Go to forums
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
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!”;}
};
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency