Computer Choice Part 2 Syntax error


#1


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


I keep getting this error message: SyntaxError: Unexpected token else
What am I doing wrong? Thank you


var userChoice = prompt("Do you choose rock, paper, or scissors?");
var computerChoice = Math.random();

if (computerChoice <=0.33)
{
    computerChoice = "rock";
}
if else (computerChoice >0.34 or <0.66)
{
    computerChoice = "paper";
}
else
{
    computerChoice = "scissors";
};
console.log(computerChoice)


#2

I've now gotten a code that will run without any error messages popping up, but when the computerChoice gives a number that should fall under the scissors category, it chooses either rock or paper for it.

For instance, I just ran this code:

var userChoice = prompt ("Do you choose rock, paper or scissors?")
var computerChoice = Math.random();

console.log(computerChoice);

if (computerChoice <= 0.33) {
console.log("rock");
}
else if (computerChoice = 0.34-0.66) {
console.log("paper");
}
else {
console.log("scissors");
};

And got this response:
0.8855704631070715
paper

It should be scissors at that number, correct?


#3

The above is very close to correct with one exception...

if else (computerChoice > 0.34 or < 0.66)

which should read,

else if (computerChoice < 0.67)

#4

var userChoice = prompt("Do you choose rock, paper or scissors?")
var computerChoice = Math.random();
console.log(computerChoice);
if (0 <= computerChoice <= 0.33) {
console.log("rock")
}
else if (0.34 <= computerChoice <= 0.66){
console.log("paper")
}
if (0.67<= computerChoice <= 1){
console.log("scissors")
}

Hi! I used ---> else if (0.34 <= computerChoice <= 0.66). Any chance you know whats wrong with my code? Thanks! :smiley:


#5

Yes. We can see a problem.

(0.34 <= computerChoice <= 0.66)

Let's first explore the above expression...

 > computerChoice = Math.random()
=> 0.37454011430963874
 > (0.34 <= computerChoice <= 0.66)
=> false
 > computerChoice = Math.random()
=> 0.18343478767201304
 > (0.34 <= computerChoice <= 0.66)
=> true

Even though it does not throw an exception, it does not behave as one might expect.

In the first example, computerChioice is greater than 0.34 and less than 0.66 but returns false. Clearly something isn't right.

The second example returns true when computerChoice is less than 0.34 and less than 0.66. Again, strange.

Working with logic like this would be a huge hairpuller. JavaScript lets us dial in our logic with logical operators, &&, || and ! (AND, OR, NOT).

(0.33 < computerChoice && computerChoice < 0.67)

Testing...

 > computerChoice = Math.random()
=> 0.7319939383305609
 > (0.33 < computerChoice && computerChoice < 0.67)
=> false
 > computerChoice = Math.random()
=> 0.5986584862694144
 > (0.33 < computerChoice && computerChoice < 0.67)
=> true

That is more like what we expect our code to do. Problem solved.

It doesn't have to stop here, though. We know we are working with three intervals, which when examined in order from lowest to highest can be done in a simple if-else if-else statement.

Let c = computerChoice // for brevity

if (c < 1/3) {

}
else if (c < 2/3) {

}
else {

}

#6

Thank you!!! Finally got it right :smiley:


#8

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