[Lesson 8] Wrong result appears in the console


#1

Hi guys!

I do not know where I am wrong even though I have checked my code many times.

Here is my commented code:

// Question to the user

var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("User:" + " " + userChoice);

// Computer's choice

var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

// Compare User and Computer's choice

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

Here is the result in the console:

User: Rock
Computer: scissors
"Paper wins"

#2

Oh I got it, It was just a question of Scissors which is different from scissors...


#3

Hi @pycoder35145,

If it may help please see the code example below. In short you are comparing two different variables as far as JavaScript is concerned. You have selected one form of uppercase EG: 'Rock' and are comparing against one form lower case EG: 'rock'.
We can introduce a simple method in to uniform you prompt variable, and then adjust your code so it matches and you should be rocking and rolling.

// Question to the user

var userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase(); //lets adjust our prompt var so it is uniformed
console.log("User:" + " " + userChoice);

// Computer's choice

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

// Compare User and Computer's choice

var compare = function(choice1,choice2) {
    if (choice1 === choice2){
        return "The result is a tie!";
        }
    else if (choice1 === "rock") { //respectively let's adjust comaprision so it matches our prompt
        if (choice2 === "scissors") { //same for the choice we set the computer
            return "Rock wins";
            }
        else {
            return "paper wins";
            }
        }
    else if (choice1 === "paper") { //adjust again
        if (choice2 === "rock") { //adjust again
            return "Paper wins";
            }
        else {
            return "Scissors wins";
            }
        }
    else if (choice1 === "scissors"){ //adjust again
        if (choice2 === "rock") { //adjust again
            return "Rock wins";
            }
        else {
            return "Scissors wins";
            }
        }
    };
    
compare(userChoice,computerChoice);

#4

Oh nice job debugging, you worked it out. I will leave my answer here simply because it shows the method in use and gives an explanation for anyone else who may need the input.