What if choice 1 is scissors



This is the error message i get when running the code below.
"Oops, try again. Your code returned 'scissor wins' instead of 'undefined' when the inputs are scissors and paper"

I have looked all over all the topics but can't find what;s going wrong. Anyone here who could help me pleasse?

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 === "paper") {
            return "scissor wins";
        else {
            return"rock wins";

compare(userChoice, computerChoice);


Look to your code instead. Which part of your code is responsible for that decision? The error message is a little buggy, but even so it is pinpointing where the problem is.


Okay I fixed it by deleting the last block. The problem is I just don't understand why I should have done that.
Could you please explain the logic behind this move?
That would be greatly appreciated.


Maybe you shouldn't have. Did that removed code have some purpose that isn't covered by anything else? If so, it should have stayed where it was, but perhaps it needed some adjusting. Or perhaps some other part needed adjusting, there are two separate places where the choices paper and scissors get handled and in both places it needs to decide that scissors is the winner.


Just noticed that it isnt complete than yeah.
Will start the lesson again to build it from scratch again to get a better feeling

Thanks for the help so far


Repairing what you have is good practise though - you will always have bugs, you will always need to fix things too large to rewrite!


Actually, you know what. My bad.
You're picking the right choice.
But you're returning a different string from what it wants, it just wants "scissors", not "scissors wins"

A corrected error message would be:
"Oops, try again. Your code returned 'scissor wins' instead of 'scissors' when the inputs are scissors and paper"


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