Illegal return statement?


#1

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

why is this an illegal return statement? how do i fix it?


#2

Next time format your code please.

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

if (computerChoice < 0.34) {
  computerChoice = "rock";
  return computerChoice; 
} else if (0.34 <= computerChoice && computerChoice <= 0.66) {
  computerChoice = "rock";
  return computerChoice; 
} else {
  computerChoice = "rock";
  return computerChoice; 
}
console.log (computerChoice);

Now, do you see there error? You miss ";" almost everywhere. Also, why you have "{}" after console.log()? You could and should start "if" right away, without putting it inside "{}". Also, your "if" statements are written wrong. What does 0.67<= computerChoice <=1 mean? Did you wanted to test this 0.67<= && computerChoice <=1? Next, why are you returning computerChoice = scissors? Your probably want to return string value "scissors". In your "else" part of if statement you have a condition. If you want conditions, use else if. You don't want it in this part. I've also put console.log() to the end so that you actually get a string output of what computer played (you was getting a number between 0 and 1 before.


#3

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/return
http://stackoverflow.com/questions/22134753/return-statement-in-javascript


#4

it still says illegal return statement


#5

Hi on your code try to remove the condition on the else statemnt and all your return statement
and use that for your else if statemnt else if(0.34 <= computerChoice <= 0.66)`


#6

Ok, I've investigated it a bit more. It seem that code like this works

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

if (computerChoice >= 0 && computerChoice < 0.34) {
  computerChoice = "rock";
} else if (0.34 <= computerChoice && computerChoice <= 0.66) {
  computerChoice = "rock";
} else {
  computerChoice = "rock";
}
console.log(computerChoice);

So there is actually no need for return, which makes sense because return stops the execution of a function. My bad, I just tested it in codepen earlier to check my syntax, didn't try to test it in Codecademy environment.