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


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);


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?



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:


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


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


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


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


thank you for your opinion


thank you for your help


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