Problem with my syntax


#1



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


I can't find the syntax error. I'm on no_4 and it keeps giving me a "there was a problem with your syntax" error message.


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

if (0 <= computerChoice <= 0.33) {
    computerChoice = "rock";
}

else if (0.34 <= computerChoice <= 0.66) {
    computerChoice = "paper";
}

else (0.67 <= computerChoice <= 1) {
    computerChoice = "scissors";
}


#2

You should learn to end your code with semi-colon. Also,

You could be confusing the computer. Why not:

if(computerChoice <= 0.33)

And this just be: else if (computerChoice >= 0.34 && computerChoice <= 0.67 )

This last else should be a final alternative when all others failed.


#3

This expression makes sense on paper, as in a math inequality...

0 <= a <= 0.33

but does not apply in JavaScript. We need to write two separate expression, and use &&.

0 <= a && a <= 0.33

However, since the order of testing is from lowest to highest, we only need to use a simple expression:

if ( c <= 0.33) {

}
else if ( c < 0.67 ) {

}
else {

}

#4

I edited the code just like you said but I'm still getting the error message.

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

if (computerChoice <= 0.33) {
computerChoice = "rock";
};

else if (computerChoice =>0.34 && computerChoice <= 0.67) {
computerChoice = "paper";
};

else (computerChoice => 0.68 && computerChoice <= 1) {
computerChoice = "scissors";
};

console.log( computer chooses + "" + computerChoice);


#5

I tried what you suggested but I'm still getting the error message. do you have any more pointers?

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

if (computerChoice <= 0.33) {
computerChoice = "rock";
};

else if (computerChoice < 0.67) {
computerChoice = "paper";
};

else (computerChoice <= 1) {
computerChoice = "scissors";
};

console.log( computer chooses + "" + computerChoice);

Also I know the semi-colons are not supposed to come after the brackets but every time I don't include them I get an error message telling me that I'm missing semi-colons.


#6

Remove any semi-colons that precede an else. We never write semi-colon in an if-else if-else structure. See my earlier example.


#7

Please do as suggested by @mtf and let's hear your result.

Thank you.


#8

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