Next Steps ReferenceError: scissors is not defined


#1

indent preformatted text by 4 spaces
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) {
prompt ("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 (choice2 === "rock" ) {
        return("rock wins");
    }
    else {
        return("scissors wins");
    };
};

};

compare(userChoice,computerChoice)

The code works, UNLESS i enter rock as userChoice input, which is when I get "ReferenceError: scissors is not defined".

Can somebody please help? thanks!


#2

@yashnair95,

-1

You will have to use a string Value instead of pointing to a variable in your =choice= comparison
thus change

(choice2 === scissors)

into

(choice2 === "scissors")

-2

Do not use semi-colon's-; in your IF ELSE statement,
a semi-colon-; is an End-of-Statement indicator ( not used in IF statement construct )
http://stackoverflow.com/questions/17036135/why-is-it-that-semicolons-are-not-used-after-if-else-statements

References::

http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript


#3

@leonhard_wettengmx_n

Thank you very much! I can't believe I overlooked that particular set of quotation marks, suppose it's what happens when you're learning in the wee hours. The code works perfectly now!

As for the issue of semi-colons in the if-else statements I think I was a little confused regarding the redundancies wrt curly brackets and semicolons, will work on it as a future habit. Thanks for all the sources as well!


#4

@yashnair95,


read the crockford code convention...


Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com


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 )