What happened with my code?


#1

Hi all,

I just finished my course for the rock, paper, scissors game but I think I am doing something wrong with my code. I just tried it out, and inputted paper, but the computer returns rock, and it says "scissors wins." What is wrong with my code? How do I fix it? My code below:

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(choice2 === "Scissors"){
            return "Rock wins";
        }
        else{
            return "Paper wins";
        }
    }
    else if(choice1 === "Paper"){
        if(choice2 === "Rock"){
            return "Paper wins";
        }
        else{
            return "Scissors wins";
        }
    }
    
};

console.log(compare(userChoice, computerChoice))

#2

First, in your else if you should put condition like this else if(computerChoice > 0.33 && computerChoice <= 0.67), just as a safety measure, to properly cover the random range for rock.
Also, you didn't cover the situation where your choice1 in function (userChoice is sent there) is scissors. I will add that too.
Now, to the original problem.
In first part of your program, where you are assigning string values to random numeric values of computerChoice, you assign them in lowercase. In second part, when you are comparing the choice, you are comparing it with first capital letter. If you compare rock and Rock, it will return false.
So my suggestion is to keep string that you will compare in lowercase in all places. To ensure that rock is equal to Rock, you can convert chosen values to lowercase first (that is, if you decide to attach lowercase strings to random numeric values for computerChoice).

Here is the code that is finished and working, as long as you input userChoice as lowercase string.

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("User: "+ userChoice +" Computer: " + computerChoice);

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 if(choice2 === "paper") {
            return "Paper wins!";
        }
    } else if (choice1 === "paper"){
        if(choice2 === "scissors"){
            return "Scissors wins!";
        } else if(choice2 === "rock") {
            return "Paper wins!";
        }
    } else if (choice1 === "scissors"){
        if(choice2 === "paper"){
            return "Scissors wins!";
        } else if(choice2 === "rock") {
            return "Rock wins!";
        }
    }
}

compare(userChoice, computerChoice);