This forum is now read-only. Please use our new forums! Go to forums
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
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.
Answer 5590a4b4e0a3007ff9000cba
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.
Answer 55c118479113cbbc83000021
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”; };
6 comments
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.
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
paper is the middle interval, < 0.66. Scissors is the default when not rock or paper.
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 // }
Thanks bcsprecher… this saved me!
thanks this helped me alot
Answer 55f32bf8937676dca3000340
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";
}
Answer 55b5a59976b8febd980004f3
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.
1 comments
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
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”; }
5 comments
(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.
“…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!
@Roy you are awesome. thanks
Answer 55f1b8a0e39efe133200032b
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);
2 comments
Remove: ( computerChoice > 0.67 ). else does not have a conditional.
ok ty :)
Answer 56120ea79113cbdaf000010d
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”
Answer 561217993e0ec83d0900022c
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.
2 comments
‘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.
thanks for the input there Roy. I strive to be as awesome as you
Answer 56150f58da5b6a59e4000061
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”; };
2 comments
whats wrong here. they say unexpected else token
Remove the semi-colons after all the braces. }; should be } in an if statement.
Answer 5615281de39efe437a00024e
// 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” }
Answer 5615fc59b625488193000293
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’; };
1 comments
Take out the semi-colons at the end of each line, }; should be }
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
17 comments
Roy,
You were right i removed my answer and gave your comment a +1
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.
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! :)
Thank you! I was thinking the same about the missing “random” numbers: between 0.33 and 0.34, 0.66 and 0.67!
thank you with it saying between .34 and .67 im trying to think of how to put it c >= .34 and < .67
Thank you, very helpful
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 :)
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.
Wow, thank you! This was so helpful. What a nice community. :)
thanks!
Thank you Roy!!! I was stuck for two decent hours, I was on the verge of a meltdown.
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.
Order matters. if 1/3 else if 2/3 else default. Changing the order will change the outcome, though. You do see that correctly.
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
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…
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.
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.