Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDennis de Vries
9,440 PointsTwo Q's about my (non-working) random number generator
Hi everyone,
I took the challenge of creating a Random Number Generator and....miserably failed.
When Dave show's me his version, I do understand what's going on, so at least that's something. But this leaves me with two questions about this selection of useless characters I managed to put in the workspace:
var userNumberLow = prompt('Fill in a number');
var userNumberHoog = prompt('Now fill in a higher number');
var Output = Math.floor( Math.random() * ParsInt( userNumberHigh - userNumberLow + 1 ) ) + ParseInt(userNumberLow));
document.write('This is your random number: ' + output);
1: It is simply not working from the start. There is no prompt-box or anything. I checked the first two lines seperately, they gave a dialog box. According to the Console, the problem is in line 3: "SyntaxError: missing ; before statement" I don't know what to make of that. There is a ';' right there, but obviously I missed something.
2: Because it is not working, I didn't get to check the code. I'm mostly curious about the way the ParseInt is integrated. Dave did this right after creating the variable,. For some reason I chose to wait till the end. It kinda makes sense to me this way, but I also would not be surprised if this is all wrong and wouldn't work.
Any help or advice is greatly appreciated! Dennis
4 Answers
Nicolas Jacobson
2,732 PointsYou've got a couple issues.
There were a few syntax errors in there. Make sure that you are spelling certain things correctly ( ie parseInt() ). Your calculation will not come out with the proper result either. For example, the parseInt() section inside of Math.floor() isn't doing what you want it to. It is trying to subtract a String from a String and then add one--and THEN turn the String into an integer. Remember, prompt() will always return a String.
Try reducing each of your variables down to a number. That way you can try solving your equation without the distraction of trying to parseInt(). Also be very careful of how you are spelling your variables and using syntax.
geoffrey
28,736 PointsHi Dennis, I've just checked your code, there are several minor mistakes.
Firstly, pay attention with the name you give to your variables and even more when you need to use them after you declared them. If I tell that, that's because you declared in your code a variable called userNumberHoog and then you use later on a variable called userNumberHigh (Which I guess is the name you wanted to give at first when you declared it).
Next, the first error you pointed us "Syntaxe etc...." is because you added an unnecessary closing parenthesis in one of the parseInt() function.
Lastly, pay attention to the character case when you use methods or native Javascript functions, you several time use ParseInt() insteaf of parseInt() which is the right way.
Here is the code corrected, this way you can compare:
var userNumberLow = prompt('Fill in a number');
var userNumberHigh = prompt('Now fill in a higher number');
var Output = Math.floor( Math.random() * parseInt( userNumberHigh - userNumberLow + 1 ) ) + parseInt(userNumberLow);
document.write('This is your random number: ' + Output);
Keep it up !
Dennis de Vries
9,440 Pointsgeoffrey Thanks so much for the explanation and apologies for the typing errors. Those are just silly and unnecessary (yet I can't seem to spot them myself). One more question, though: I was under the impression that the use of capitals is merely for readability. Does the difference between ParseInt and parseInt have actual consequences for the code, or is it exactly that: for readability?
Nicolas Jacobson "It is trying to subtract a String from a String and then add one--and THEN turn the String into an integer."
Aha, this was what I was afraid to mis, but didn't know I missed yet. Thank you for that, explains it all.
geoffrey
28,736 PointsYes that has actually a consequence, ParseInt is undefined, it doesn't exist, unless you create that function yourself, while parseInt does exist.
You can quickly test it, just open a console in your browser and type ParseInt("1515"); Google Chrome will return this message VM301:2 Uncaught ReferenceError: ParseInt is not defined, however, parseint("155151") will actually works, because this is an existing function a native Javascript function !
Don't be sorry for the typings errors, sometimes, when you spend too much time on a exercise you don't see obvious things. In this case I guess It's better to take a little break and then to come back with a fresh mind.
Dennis de Vries
9,440 Pointsgeoffrey Did the test in the console and yes, you are right. And your explanation makes a lot of sense. Thanks so much for taking the time clearing things up. Much appreciated.
geoffrey
28,736 PointsYes It's simply because Javascript is case sensitive ! :) Np here, always glad to help when I'm able to ;)