8. What if choice1 is scissors?


#1

https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661#

Hi! My code works when I hit "Save & Submit Code", and the I get an answer returned, ex. "Computer: paper paper wins", yet I keep getting the error message "Oops, try again. Your code returned 'rock wins' instead of 'undefined' when the inputs are scissors and paper". Perhaps I misread the instructions? I studied all of my if/else functions, but I can't find an error (I don't have "rock wins" as the return on anywhere that has inputs as scissors and paper, unless I'm just skimming over it?). Anyhow, here's the entire code that I've got.

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 "scissors wins";
            }
   }
};
compare(userChoice, computerChoice);

Plus, I'm not sure if I'm missing any curly brackets, or if I have too many anywhere, although I'm not getting any error messages regarding that. Thank you!:slightly_smiling:

I don't know how to delete this thing, but I figured out my issue was not formatting the last else if statement. (I used one = rather than three on the else if(choice1==="scissors") and if(choice2==="rock")...hahah


#2

@tree_octopus,

Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on:

  • the creation of the compare function
  • the truth-table
  • the build of the IF ELSE-IF statement structure as part of the compare FUNCTION-BODY
  • the execution of the compare function (keeping in mind that the return statement is used )

#3

The difference that I see in your script compared to what I got to work was not to use an Else If statement for when choice1 value is "scissors". It appears that an Else statement would assume that choice1 is "scissors", the only option remaining to be tested, generates the expected outcomes.


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

If this is the proper approach expected by this lesson, a humble suggestion would be to modify the lesson's directions not to mislead students into utilizing the Else If statement.