Stuck though code perfectly working!


#1

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

Hello there!

I can't figure out why I'm stuck with this exercise.
When I submit the code I get the message
"Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper"
Even when my code DOES NOT return 'paper wins'.
The code works perfectly as it should on the website, but I keep receiving this message and can't go on with the course.
Even in a browser the code works and the console reports no bugs.

Please Help!

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

var result= compare(userChoice, computerChoice);

console.log(result);


#2

Hi the instrcution aske you to ..

if choice1 is "scissors"?
a. if choice2 === "rock", then "rock" wins.
b. if choice2 === "paper", then "scissors" wins. (this will be the else statement)


#3

Hi!
In fact I wrote the last 'else if' statement intuitively, not the exact way CodeAcademy wants it.
I wrote:

else if(choice1 === "scissors"){
if(choice2 === "paper"){
return "paper wins";
}else{
return "rock wins";
}

When the exercise was asking for this:

else if(choice1 === "scissors"){
if(choice2 === "rock"){
return "rock wins";
}else{
return "scissors wins";
}

They both make perfect sense and work flawlessly in a browser, but I didn't know the software on the website wants the exact order.
It's like missing an exclamation mark in a string when it is expected.

Thank you very much for your help!!!


#4

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