Rock, paper, scissors, lizard, spock

javascript

#1



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


So I tried making Rock, paper, scissors, lizard, spock by using only what I knew from the previous tutorials but I am stuck. I only get a result when I pick "rock", everything else comes back as undefined. Can anyone help with this?


var userChoice = prompt("Do you choose rock, paper, scissors, lizard, spock?");
var computerChoice = Math.random();
if (computerChoice < 0.2) {
    computerChoice = "rock";
} else if (computerChoice <= 0.4) {
    computerChoice = "paper";
} else if (computerChoice <= .6) {
    computerChoice = "scissors";
} else if (computerChoice <= .8) {
    computerChoice = "lizard";
} else {
    computerChoice = "spock";
}
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
 if (choice1 === choice2) {
return userChoice = prompt("The result is a tie!Please make a new choice.");
}else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
} else if (choice2 === "lizard") {
return "rock wins";
} else if (choice2 === "spock") {
return "spock wins";
} else if (choice2 === "paper") {
return "paper wins";
}if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else if (choice2 === "spock") {
return "paper wins";
} else if (choice2 === "scissors") {
return "scissors wins";
} else if (choice2 === "lizard") {
return "lizard wins";
}if (choice1 === "scissors") {
if (choice2 === "paper") {
return "scissors wins";
} else if (choice2 === "lizard") {
return "scissors wins";
} else if (choice2 === "spock") {
return "spock wins";
} else if (choice2 === "rock") {
return "rock wins";
}if (choice1 === "lizard") {
if (choice2 === "spock") {
return "lizard wins";
} else if (choice2 === "paper") {
return "lizard wins";
} else if (choice2 === "scissors") {
return "scissors wins";
} else if (choice2 === "rock") {
return "rock wins";
}if (choice1 === "spock") {
if (choice2 === "scissors") {
return "spock wins";
} else if (choice2 === "rock") {
return "spock wins";
} else if (choice2 === "paper") {
return "paper wins";
} else if (choice2 === "lizard") {
return "lizard wins";}
}}}}}};

console.log(compare(userChoice, computerChoice))


#2

the moment a return keyword is reached, a function ends, so the return on this line:

return userChoice = prompt("The result is a tie!Please make a new choice.");

means the function ends, even before you start comparing. Now, the idea is that you check what choice1 is:

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

}

inside this else if condition, check what choice2. It is important to close else if choice1 rock before moving on to next, you didn't

else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
} else if (choice2 === "lizard") {
return "rock wins";
} else if (choice2 === "spock") {
return "spock wins";
} else if (choice2 === "paper") {
return "paper wins";
}
/* missing } to close else if choice1 rock */
if (choice1 === "paper") {

see comment. you really this structure, wrong nesting means choices aren't evaluated properly.


#3

Thank you, it finally works. now if I can get it to work properly when its a tie. Seems like every time it is a tie, even after my second input the computer chooses the same answer as me.


#4

that makes sense, you never generate a new random number and let the computer pick something new