What if choice1 is paper (another one im sorry)


#1

I seem to be stuck on this part of the lesson. Ive tried many different JS programs to scan my code ( repl.it JS Bin, online JS beautifier, JSLint) They usually come back with - Syntax error "return" outside of function. I have noticed a "bug" where if you just click save and submit code and you at least completed "what if choice1 is rock" and do nothing for the next two sections, CC says you successfully completed said courses.

Here is my code

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";
        }

    }

};

{
    if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }
};

#2

@xlxkovuxlx now I believe your issue is one because you have an extra "};" and that instead of having more else if statements you have if ones.
I would suggest your code be like this.

return "paper wins";
}

}

{
else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
}

What this does is deletes two unnecessary end statements and changes the paper to else if instead of if.


#3

Thank you for your quick reply. This led me in the right direction. I ended up putting my code like this:

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";
}

}

{


  if(choice1 === "paper") {
    if(choice2 === "rock") {
        return "paper wins";
}else {
        return "scissors wins";
    }
}
}
};

Not sure if this is a fluke or not because I still don't understand. But this is what I did and it worked


#4

@xlxkovuxlx I believe your issue was because you had an extra "};" I am not completely sure though.


#5

Your probably right. Nonetheless thank you very much for your help.


#6

@xlxkovuxlx no problem, I really enjoy fixing people issues in their code, it helps me further my own coding skills myself.


#7

The problem in the original code was that you closed your function too early:

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";
        } // closes else

    }// closes else if

}; closes function

This probably gave you an error about an unexpected else, for which you found the workaround of using an if. Unfortunately this is not a solution as the original problem of this case not being inside of the function isn't solve you you should therefore again get errors about not defined choice1 and choice2 and a not allowed use of return.

The last try in here is much better but why do you have these unnecessary {} around your second else if? Also as you're now back inside the function you can use the else if instead of the two if/if again :slightly_smiling: