Always returns "rock wins"


#1


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


I don't have error messages, but everytime after I check my code, it will return the right answer and right after that it will always return "rock wins".
I checked when it returns the extra answer, and it's the last alert says "rock wins".


Replace this line with your code. 
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();

if(computerChoice < 0.33) {
    computerChoice = "rock";
}else if (computerChoice <= 0.67){
    computerChoice = "paper";
}else{
    computerChoice = "scissors";
}confirm("Computer: " + computerChoice);

var compare = function (choice1, choice2) {
    if(choice1 === choice2) {
      alert("The result is a tie!");
    
    }else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            alert("rock wins");
    }else{
            alert("paper wins");
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            alert("paper wins");
            }else{
                alert("scissors wins");
       }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            alert("scissors wins");
            }else{
                alert("rock wins");
        }
    }
}
compare(userChoice, computerChoice)


#2

@silvercorporal,
If you use the Submit button
the code is run
and
the code you defined is checked by a code-checker.....
it does it's check's and will lead to your last-output.

===============================================

What should be said....
The requirement was to use the return statement instead of the alert() statement....
If you call the compare function, in which you use the return statement
the returned Result has No-Display

Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on:

  • the execution of the compare function (keeping in mind that the return statement is used )

#3

Thanks for the reply :slight_smile:

I really want to use the alert() statement, will I learn how to use it with functions later on the course?


#4

@silvercorporal,
Do a google search
== discussions / opinions ==
alert() vs return site:stackoverflow.com
and read some of the articles......


#5

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