Lesson 8 - "Oops, try again" errors don't match the output


#1



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


I'm having trouble understanding what went wrong in my code to cause the error messages not to match the output after submitting my code. I've listed a couple of examples below:

Ex.) When I enter "rock" and the computer enters "paper", the output says that paper wins. However, I get an error that says "Oops, try again. Your code returned 'scissors wins' instead of 'undefined' when the inputs are scissors and rock"

Ex) When I enter "paper" and the computer chooses "paper", the output says "The result is a tie!". However, I get an error that says "Oops, try again. Your code returned 'scissors wins' instead of 'undefined' when the inputs are scissors and rock"

No matter what I enter and no matter what the computer enters, I continue to run into this problem. What am I missing that is causing this? Thanks!


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

compare(userChoice, computerChoice);


#2

I think your compare function would run if you had declared variables called choice1 and choice2. In this exercise, you have declared userChoice and computerChoice. So, choice1 and choice2 are undefined.

What can you do to your code that would allow the compare function to use defined variables?
How do you print the compare function to the console?


#3

So in bigger context you should have if{} then else if{} and then else{}. This last part is where you got - if(choice1 === "scissors") {} and it should be a else {}.

Also within that last part of yours you have two if{} and it should be a if{} else{}.


#4

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