Rock,paper,scissor code


#1

Please help. !

var choice1 = prompt("Do you choose rock,paper or scissors?");
var choice2 = Math.random();
if(choice2 < 0.33) {
    choice2 = "rock";
} else if (choice2 >= 0.34 && choice2 <= 0.66) {
    choice2 = "paper";
} else { 
    choice2 = "scissors";
} console.log("Computer: " + choice2);
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";
    }

}}
console.log(compare(choice1,choice2));

On executing this, after i enter input as paper, it gives me undefined error.


#2

well so far you only have coded a tie and if you enter rock, so it makes sense an error arise when entering paper? Your code can't handle it yet, you will work on this in the next few lessons

so enter rock


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.