8. What if choice1 is scissors?


#1

Hey everyone, working my way through this JavaScript corset for the second time to get a better understanding... I can't figure out why I'm getting an "expected ';'" error, I've tried looking at other people's questions and the answers and I've looked through "correct" code but I must be missing something.. can someone find it?

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

compare (userChoice, computerChoice);

Thanks!


#2

@mikegl,

Have a close look at

else .. (choice1 === "scissors") {

And for the execution of your compare =function=
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

@leonhard.wettengmx.n I deleted the condition (choice1 === "scissors" from that part, thought that looked weird.. that worked to let me pass and the code worked as it should.. what were you referring to as far as the execution of my compare function? Was that just for my understanding of why to remove the condition or am I missing something else? Just here to learn, I appreciate your help!


#4

@mikegl,

1

Instead of removing the condition
you had to include an IF so that you would have the 3th ELSE IF :sunglasses:

2

As your compare function is using the return statement
you will get a Result with NODisplay.
To get the Result _Displayed you have to use

console.log( compare( userChoice, computerChoice) );

#5

@leonhard.wettengmx.n

1.) Ah, okay I see.. didn't know I could do an if, else if, else if, else if.... thought I had to end it with an else...

2.) I don't fully understand this one. I tried the code both with console.log and without it, just calling the compare function as compare(userChoice, computerChoice); and both did the exact same thing. Do you have an explanation for this?

Thanks so much for your help, you seem to always come through each time I post a question! :smiley:


#6

@mikegl,
In the course-environment the return is Displayed
Try it somewhere else....

nice to know

You could test in labs.codecademy.com
choose Javascript
You will get a split-screen
In the left-hand-side you paste-in your code
click on the Run button.
The right-hand-side is the console

references

www.crockford.com --> for most respected guide-line in Javascript
like coding convention
www.crockford.com
http://javascript.crockford.com/code.html <== Javascript code-convention
http://javascript.crockford.com/survey.html