Script not running from "var compare" and downwards


#1

Hello, can somebody help me with my code. I already completed the Rock, paper, scissors lesson, however it is only running the first part of the code – more specifically its only running the choice of the user and the computer, not the result of the comparison.

Here is my code:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}

console.log("Computer: " + computerChoice);

var compare = function(choice1,choice2) {
if(choice1 === choice2){
return "The result is a tie!";
}
else if(choice1 === "ROCK"){

if(coice2 === "SCICCORS"){
    return "ROCK wins";
}
else
{
    return "PAPER wins";
}

}
else if(choice1 === "PAPER"){

if(coice2 === "ROCK"){
    return "PAPER wins";
}
else
{
    return "SCISSORS wins";
}

}
else if(choice1 === "SCISSORS"){

if(coice2 === "PAPER"){
    return "SCIRRORS wins";
}
else
{
    return "ROCK wins";
}

}
};

THANKS


#2

Hi,

first i see that you didnt call your function compare

compare(computerChoice, userChoice);

Second you know JS is casesensitive ?!
Your script will compare
ROCK === rock // false
PAPER === paper // false
SCISSORS === scissors // false

So no code from your if/ else if statements will execute

And you use three different types of typing scissors.
scissors
SCICCORS
SCISSORS


#3

you could put an uppercase function around prompt to solve the problem and is probably a better solution that what you are asked to do.


#4

Solved! Thanks dookoo_de!


#5

Just one pointer you will have an error when the result is not a tie.

Check out you code on labs
Click here
or here it is

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

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 === "SCICCORS"){return "ROCK wins";}
                        else{return "PAPER wins";}
                    }
                    else if(choice1 === "PAPER"){
                        if(choice2 === "ROCK"){return "PAPER wins";}
                        else{return "SCISSORS wins";}
                    }
                    else if(choice1 === "SCISSORS"){
                        if(choice2 === "PAPER"){return "SCIRRORS wins";}//the error was in the if condition here
                    else{return "ROCK wins";}
                    }
            };
            
console.log("Computer: " + computerChoice);
console.log("Your Choice: " + userChoice);
console.log(compare(userChoice,computerChoice));

Unexpected number error