This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Paul Pearson
over 8 years

4/9 SyntaxError: Illegal return statement

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

var computerChoice = Math.random(); console.log(computerChoice);

if (computerChoice <= 0.33) { return “rock”; } else if (computerChoice >= 0.34 || <= 0.66) { return “paper”; } else { return “scissors”; }

Answer 5590a319e0a3006892000d0d

93 votes

Permalink

This piece of lesson text is causing so much confusion, I really wish CC would revise it:

01. If computerChoice is between 0 and 0.33, make computerChoice equal to “rock”. 02. If computerChoice is between 0.34 and 0.66, make computerChoice equal to “paper”. 03. If computerChoice is between 0.67 and 1, make computerChoice equal to “scissors”.

By this reasoning, we miss fully 2% of the sample space if we set up our code this way. 0.33 to 0.34, 1% of sample space. 0.66 to 0.67, another 1% of sample space.

What if Math.random() is 0.331? computerChoice does not get set to a string but remains a number. Same with 0.33999999999, 0.661, 0.6699999999, and everything in between 0.33 and 0.34, and, 0.66 and 0.67.

This lesson text suggests to learners that their code should be written with logical expressions, when it is completely unnecessary.

How do we divide 1 into three parts? Divide by 3

1/3 + 1/3 + 1/3 == 1

Since Math.random() is never less than 0, but may equal 0, we do not need to test for 0, just less than 1/3. Failing this, we next test if the value is less than 2/3. Failing this, we can go with a default since we know that Math.random() is always less than 1. There is very little logic required:

// LET c = computerChoice for brevity
var c = computerChoice;
if (c < 1/3){
    c = "rock";
} else
if (c < 2/3){
    c = "paper";
} else {
    c = "scissors";
}
computerChoice = c;

Or we could write it with decimal fractions:

if (c <= 0.33){
    //
} else
if (c < 0.67){
    //
} else {
    //
}

If I had one wish, it would be to give back all the thousands of hours that have been wasted by learners and helpers alike on this one lesson.

points
Submitted by Roy
over 8 years

17 comments

Floris Schmidt over 8 years

Roy,

You were right i removed my answer and gave your comment a +1

Roy over 8 years

Whatever that comment was… I’m getting old and my memory is not what it once was. Hope you’re okay with removing whatever you removed and glad you are good with what I said. That’s a nice take away. Thanks.

skylynn20 over 8 years

Wow, thank you so much for this explanation! I keep seeing your answers on these forums when I get stuck and they’re always so helpful and detailed. Thank you again; you’re awesome. Keep doing what you’re doing! :)

Marina Stewart over 8 years

Thank you! I was thinking the same about the missing “random” numbers: between 0.33 and 0.34, 0.66 and 0.67!

Christopher Davies over 8 years

thank you with it saying between .34 and .67 im trying to think of how to put it c >= .34 and < .67

Steph over 8 years

Thank you, very helpful

Saba Lekveishvili over 8 years

I tried this solution, but the CC error popped up and said I didn’t use .33 and .67 in ranges. so I just did this:

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

and to my amusement, this code worked :)

Roy over 8 years

I’m not absolutely sure, but I think there is one lesson that checks for the decimal fractions. The rest let anything slide, such as 1/3 and 2/3.

YoshiCodes over 8 years

Wow, thank you! This was so helpful. What a nice community. :)

sharylous over 8 years

thanks!

Diana De Jesus over 8 years

Thank you Roy!!! I was stuck for two decent hours, I was on the verge of a meltdown.

Ahmed Rakib Imran over 8 years

well didn’t you miss some points? here they say,”If computerChoice is between 0 and 0.33, make computerChoice equal to “rock”.” and when you’re writing the above code, clearly c<2/3 doesn’t mean its greater than 0.34 and less than 0.67 , it means it can be any number less than 2/3 or 0.67. So can you explain this? that would be a great help :) thanks.

Roy over 8 years

Order matters. if 1/3 else if 2/3 else default. Changing the order will change the outcome, though. You do see that correctly.

Pharopr over 8 years

I personally do not check Q&A but being that 2 weeks is a long time I had to check this out a million thanks Roy ur a life saver

theotab over 8 years

I was stuck primarily on the ‘between X and Y’ and how to write that out. I’ve been learning code for a total of 3 hours. How am I supposed to indicate ‘between’? I logically tried var userChoice = prompt(“Do you choose rock, paper or scissors?”) var computerChoice = Math.random() if (computerChoice >= 0 && <=0.33 ) { return = “rock”; } else if (computerChoice >= 0.34 && <=0.66) { return = “paper”; } else (computerChoice >= 0.67 && <=1) { return = “scissors”; } But, didn’t work. I don’t believe we were taught ‘between’, nor does this even cover all possible outcomes…

Erzats over 8 years

I’m surprised that not being specific about the plage of numbers available to compute one of the three choice doesn’t bug out the code.

I actually though for a few minutes that I’d have to write something like “>== 0.33, <== 0.67” to make it work.

Vayl over 8 years

I know this is extremely simple for you, but for me, this is a very elegant solution. Thanks for sharing, it’s a good reminder that “good code” is also about avoiding repetition and unnecessary clusters.

Answer 5590a4b4e0a3007ff9000cba

8 votes

Permalink

The error message received in the opening post is caused by using the return keyword outside of a function body. A function body is the only place return can appear in any code.

points
Submitted by Roy
over 8 years

2 comments

jmarq89 over 8 years

Thank you! I didn’t know that until you posted this!

suyash988 over 8 years

Thank you!

Answer 55c118479113cbbc83000021

7 votes

Permalink

Thanks for all of the help, attached is the code I was able to successfully execute.

var userChoice = prompt(“Do you choose rock, paper or scissors?”); var computerChoice = Math.random(); console.log(computerChoice); if(computerChoice < .34) { computerChoice = “rock”; } else if(computerChoice < .66) { computerChoice = “paper”; } else { computerChoice = “scissors”; };

points
Submitted by bcsprecher
over 8 years

6 comments

Roy over 8 years

While it may be accepted and runs, we should avoid writing decimal numbers without a leading 0, as in 0.33, 0.66. Best adopt this practice now before it leads to problems that are hard to find and fix.

Thecomp0ser over 8 years

I dont understand one thing, there isn’t any reason for the computer to pick between paper and scissors. from what you have written here it seems that paper and scissors are both to be picked at the same time as long as the number is above .66

Roy over 8 years

paper is the middle interval, < 0.66. Scissors is the default when not rock or paper.

Sally Summers over 8 years

Thecomposer -it’s just because it runs in order (1st condition? Nup, 2nd condition? Nup. Third condition? yes.) Don’t overthink things, they don’t ask what they haven’t already shown. Code here: var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = “rock”; } else if(computerChoice <= 0.67) { computerChoice = “paper”; } else { computerChoice = “scissors”; // <=1.0 works here too // }

Chelsea88 over 8 years

Thanks bcsprecher… this saved me!

Nyall Horner over 8 years

thanks this helped me alot

Answer 55f32bf8937676dca3000340

3 votes

Permalink

This code is accepted and works, but I need your comments on the “else if” statment

var userChoice=prompt("Do you choose rock, paper or scissors?");
var computerChoice=Math.random();
console.log(computerChoice);
if (computerChoice <=0.33) {
computerChoice="rock";
}
else if (computerChoice >=0.34  <=0.66) {
computerChoice="paper";
}
else {
computerChoice="scissors";
}
points
Submitted by Shady S
over 8 years

5 comments

Roy over 8 years

if (computerChoice <= 0.66)

Shady S over 8 years

So it doesn’t need the “>=0.34” to know the range?

Shady S over 8 years

Thank you Roy

Roy over 8 years

The lower range has been ruled out in the first branch.

arenrad over 8 years

thanks shady. good syntax

Answer 55b5a59976b8febd980004f3

0 votes

Permalink

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



if (computerChoice <= 0.33 && computerChoice >= 0) {
    computerChoice = "rock";
} else if (computerChoice <= 0.66 && computerChoice >= 0.34) {
    computerChoice = "paper";
} else { computerChoice = "scissors";; }
console.log("Computer: " + computerChoice);

here && is the logical operator.

points
Submitted by Ahmed Rakib Imran
over 8 years

1 comments

Roy over 8 years

Since you already have the lowest to highest order, why add all that unnecessary code and logic? It also has a 1% void in the sample space that would allow computerChoice to remain a float if it is > 0.33 and < 0.34.

Answer 55c5d2d8d3292f5b7e000455

0 votes

Permalink

Thanks for the help, Roy! For me, the problem was how to set the range. :D

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 (computerChoice<0.67) { computerChoice= “paper”; } else { computerChoice=”scissors”; }

points
over 8 years

5 comments

Roy over 8 years

(0<=computerChoice<=0.33) is incorrect syntax. Should be (computerChoice<=0.33)

It worked though. I guess the less you write, the better when it comes to coding.

Roy over 8 years

“…how you could have been that successful in learning coding?” __ @Jiwon, to be successful means to have a goal and never stop learning. My goal here is simple, to help young people (people in general) learn basic coding. To do that I need to keep learning, both coding skills and life skills (I’m a little rough around the edges). The key is to not give up and keep improving in the areas where we are weakest, and keep exploring in areas where we are strong. There are new doorways everywhere if we never quit. I’m just an ordinary retiree with little to give but my time and energy, and hopefully lend some confidence to learners. (P.S. I had to remove your comment to protect your privacy.)

Thanks! :) I’ve been learning coding here little by little for one and a half month, at least 6h/week. Will keep learning! Thanks for the advice!

Farzan over 8 years

@Roy you are awesome. thanks

Answer 55f1b8a0e39efe133200032b

0 votes

Permalink

What is ‘missing before statement’?

var userChoice = prompt( “Do you chose rock, paper or scissors?”);

var computerChoice = Math.random(); if ( computerChoice <= 0.33 ) { computerChoice = “rock”; } else if ( computerChoice <= 0.67 ) { computerChoice = “paper”; } else ( computerChoice > 0.67 ) { computerChoice = “scissors”; }

console.log(computerChoice);

points
Submitted by mirza cerim
over 8 years

2 comments

Roy over 8 years

Remove: ( computerChoice > 0.67 ). else does not have a conditional.

mirza cerim over 8 years

ok ty :)

Answer 56120ea79113cbdaf000010d

0 votes

Permalink

Hey Guys I figured out a way to make this work, maybe this will help:

var userChoice = prompt(“Do you chose rock, paper or scissors?”) // Popup prompt asking user

var computerChoice = Math.random();

console.log(computerChoice); // Calls the ComputerChoice variable and prints a random number in the console “ranging from 0-1”

if (computerChoice <= 0.33) { computerChoice = “rock”; } // if computer randomly picks a number less than OR equal to 0.33, then the computer outputs “rock”

else if (computerChoice < 0.66) { computerChoice = “paper”; } // if computer randomly picks a number less than to 0.66 OR GREATER THAN 0.34, then the computer outputs “paper”

else { computerChoice = “scissors”; } // if computer randomly picks ANY number GREATER THAT 0.66, then the computer outputs “scissors”

points
Submitted by Jay
over 8 years

Answer 561217993e0ec83d0900022c

0 votes

Permalink

I used the && method whereby i wrote if(computerChoice > 0 && computerChoice < 0.34){}; and it also works but thats verbose and not streamlined. although, sometimes ugly works well enough.

points
Submitted by Erika Harvey
over 8 years

2 comments

Roy over 8 years

‘well enough’ might work in a bakery but it won’t pass in Silicon Valley. It means we haven’t thought things through, but are satisfied with what we’ve arrived at. A dangerous approach to programming.

Erika Harvey over 8 years

thanks for the input there Roy. I strive to be as awesome as you

Answer 56150f58da5b6a59e4000061

0 votes

Permalink

var userChoice=prompt(“Do you choose rock,paper or scissors?”); var computerChoice=Math.random(); {console.log(computerChoice); }; if(computerChoice <=0.33) { computerChoice=”rock”; }; else if(computerChoice>=0.34 && <=0.66) { computerChoice=”paper”; }; else{ computerChoice=”scissors”; };

points
Submitted by manan parikh
over 8 years

2 comments

manan parikh over 8 years

whats wrong here. they say unexpected else token

Roy over 8 years

Remove the semi-colons after all the braces. }; should be } in an if statement.

Answer 5615281de39efe437a00024e

0 votes

Permalink

// this worked!

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

console.log(computerChoice);

if (computerChoice <=.34){ computerChoice= “rock”; } else if (computerChoice <.67){ computerChoice= “paper”; } else { computerChoice= “scissors” }

points
Submitted by Mari Valeev
over 8 years

Answer 5615fc59b625488193000293

0 votes

Permalink

Hi! I can’t see where is the problem here..Can you please help?

var userChoice = prompt ( “Do you choose rock, paper or scissors?”) var computerChoice = Math.random(); console.log (computerChoice) if (computerChoice < .33) { computerChoice = ‘rock’; }; else if (computerChoice < .67) { computerChoice =’paper’;} ; else { computerChoice =’scissors’; };

points
Submitted by Fiona Bennun
over 8 years

1 comments

Roy over 8 years

Take out the semi-colons at the end of each line, }; should be }