ComputerChoice, pt. 2


#1

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

Why is it saying "return statement outside of function"? What return statement is it referring to?

var userChoice=prompt("Choose paper, rock, or scissors?");
var computerChoice=Math.random()
if(computerChoice<0.34){
    return "rock";
}
else if(computerChoice>=0.34&&computerChoice<0.67){
    return "paper";
}
else{
    return "scissors";
}

#2

because you use return but you don't have a function? you should update computerChoice variable


#3

Do you mean like this:

var userChoice=prompt("Choose paper, rock, or scissors?")
var computerChoice=Math.random(){
if(computerChoice<0.34){
return "rock";
}
else if(computerChoice>=0.34&&computerChoice<0.67){
return "paper";
}
else{
return "scissors";
}
};

because now it's saying "SyntaxError: expected ;". Can you help me find my mistake now?


#4

I think I needed a semicolon after Math.random() so I now have:

var userChoice=prompt("Choose paper, rock, or scissors?")
var computerChoice=Math.random();
if(computerChoice<0.34){
return "rock";
}
else if(computerChoice>=0.34&&computerChoice<0.67){
return "paper";
}
else{
return "scissors";
}

I noticed there are ticks to the left of the if/else if/else statements. I tried making corrections but I get a SyntaxError each time.


#5

how do you update/re-assign variables?


#6

I'm sorry but I don't understand what you're suggesting. What do I need to reassign?


#7

In your if/else if/else statement, you're returning values, but you shouldn't, since this isn't inside a function. @stetim94 is telling you to reassign computerChoice this value, instead of returning it;

return "rock";

would be

computerChoice = "rock";

etc.


#8

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