Syntax error. Lesson 4 pt 2. HELP!


#1

I am unsure of my syntax. My code is:

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


#2

It should look more like this:

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

var computerChoice =Math.random();
if (computerChoice<=0.33) {
return("rock");
}else if ((computerChoice>=0.34) & (computerChoice<=0.66)) {
return("paper");
}else {
return("scissors");
}


#3

I tried the code and it said 'illegal return statement'. What is wrong?


#4

This is the exact code that I used. I used "alert" instead of "return", but you should be able to swap the two terms. I'm not sure what went wrong grammatically. Try this one instead perhaps.

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

var computerChoice = Math.random();

if (computerChoice<=0.33) {
    alert("rock");
} else if ((computerChoice>=0.34) & (computerChoice<=0.66)) {
    alert("paper");
} else {
    alert("scissors");
}

#5

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