Help please! I don't know why 'rock wins' always come out in the result


#1



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) {
        console.log("The result is a tie!");
    } else if (choice1 === "rock")

        if (choice2 === "scissors") {
            console.log("rock wins");
        } else {
            console.log("paper wins");
        }

    else if (choice1 === "paper")

        if (choice2 === "rock") {
            console.log("paper wins");
        } else {
            console.log("scissors wins");
        }

    else if (choice1 === "scissors")

        if (choice2 === "paper") {
            console.log("scissors wins");
        } else {
            console.log("rock wins");
        }

};

compare(userChoice, computerChoice);


#2

It doesn't seem you have posted correct code corresponding to what you expect ("... 'rock wins' always comes out in the result"). I just ran your code and it gave "The result is a tie". Maybe you can explain your problem a bit more in detail?


#3


#4

I'm guessing that's the submission test failing to redirect the output when it's testing your function.

You can verify that your code isn't printing that by itself by adding a print at the very end of your code:

console.log("================");

And if anything is printing after your code has finished, it would appear below that.


#5

Isn't compare() supposed to return a result?


#6

Yeah. This will pass:

function compare() {}

Because the submission test author didn't anticipate that the learner can return anything other than a string, in this case null


#7

The bug fixed.
Use return instead of console.log.
thank you for your help^^


#8

thank you for your opinion


#9

thank you for your help


#10

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