What is wrong with this? Will not let me proceed


#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) {
return "The result is a tie!";
}
else if(choice1 === "rock")
if(choice2 === "scissors") {
return"rock wins";
}
else if(choice1 === "paper") {
if(choice2 === "rock");
return"paper wins";
}
else {
return"scissorswins";
}
else if(choice1 === "scissors") {
if(choice2 === "rock");
return"rock wins";
}
else(choice2 === "paper")
return"scissors wins";
};
compare(userChoice,computerChoice)


#2

I put in paper
Computer : rock
Result : scissors wins
and this showed
Oops, try again. Your code returned 'rock wins' instead of 'undefined' when the inputs are scissors and paper


#3

You've got missing and wrongly placed curly braces to nest your loop controls. For instance,

I've helped correct. See below:

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


    compare(computerChoice,userChoice);

#4

I see what you did there in the last part with the curly {}.
And tried using it, still shows the same message
Except now the answer on the right side is accurate.


#5

Well since the answer is accurate but still gives the error message, pls refresh and try again. Else, please post new edited code and error displayed so I could check it out.


#6

It worked, thank you.


#7

You're welcome.

Have fun coding! :slight_smile:


#8

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