Help with JavaScript. Rock Paper Scissors Project

Hi I am on the Rock Paper and Scissors Project in Java Script, section 3. And I can not get past STEP 6, due to this error message. Is there anyone out there who can help me with, this error message?

I have uploaded an attachment. I hope it shows-up.

Thanks so much for your help.

1 Like

There are two possible reasons: The variable doesn’t exists. Which isn’t the case, I can see you declare the variable on line 14.

So that leaves us with the other option: You have a scope problem. You define the variable (rand) inside the body of the function. Then after/outside the function, you attempt to access this variable, which is not allowed, resulting in an error

Hi, Thanks for getting back to me. I’ve written the Math.random method once before, and that was when I completed the MAGIC EIGHT BALL project: When I did it then I wrote it using the let variable as follows
let randomNumber = Math.floor(Math.random ()* 8);

and it worked then, but… I was told to write it, with the Variable as Part of a Function by someone in the Support Team. And I can’t really understand why I can’t just write it, with just a variable as SHOWN Above. I have tried both ways and neither of them worked! So I Really Still Don’t Know How to get past this Problem?
Thanks for you help.

You are completely missing the point of missing my answer. The random value is generated fine. This random value is then stored in a variable. Then you try to access this variable outside its designated scope. Which results in an error

1 Like

I just feel like, I went over lesson 7 grasped the Helper Variable Concept… Completely Understand that Math Random Generates the Number and Math.floor rounds it to the nearest Decimal… But the error seems to occur when the (rand) variable is called; in the Switch Statement…! So, you say I’m accessing the variable incorrectly? Well bearing, in mind I’ve been coding for just 13days… How am I supposed to access the Variable Correctly?
Once again, sorry to take up so much of your time… if you can’t explain it to me, have you got any material or a particular lesson, that would make it clearer for me?

your comment on line 15 is wrong. } is not a square bracket, and the curly bracket/accolade doesn’t close the math random, the } closes the function

Your issue is related to variable scope, seems there are lessons on scope:

and otherwise you can consult external documentation:

1 Like

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