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 trialwei du
2,569 Pointsis the '()'position important in this question?
var userNum = parseInt(prompt('What is your number?')); var userNum_1 = parseInt(prompt('what\'s your least favor number?')); var genNum = Math.floor(Math.random() * (userNum - userNum_1 + 1)) + userNum_1; alert(genNum);
vs.
var userNum = parseInt(prompt('What is your number?')); var userNum_1 = parseInt(prompt('what\'s your leat favor number?')); var genNum = Math.floor(Math.random() * (userNum - userNum_1 + 1)+ userNum_1); alert(genNum);
please note the position of the parentheses on line 4. I have tried both ways, and both worked, but just wondering if one is better than other? Or I just got lucky on this one?
2 Answers
Milan Pankhania
3,734 PointsSo it looks like the parenthesis difference is in the Math.floor function you're talking about? In this instance the test may have passed as it might not have been looking for the correct answer value, I'm not sure, but yes where you close the parenthesis will have an effect on the result. Basically when you put anything inside the parenthesis of a method, that means that you want to do something with the code inside of it, anything outside of the parenthesis doesn't get affected.
Hope that helps?
KRIS NIKOLAISEN
54,972 PointsIt seems to me what you are essentially asking is does:
Math.floor(userNum_1) = userNum_1 ?
Since userNum_1 is a positive integer the answer is yes so the position of the parentheses are not important in this case.
Milan Pankhania
3,734 PointsMilan Pankhania
3,734 Pointsjust reformatting your code so it's easier for all to see. In the future if you're pasting in code, it's always a good idea to type 3 backticks then hit enter to go onto a new line and paste the code, then hit enter for a new line and end with 3 backticks again. You can also say what language it is which will highlight the syntax for you. Check the markdown cheatsheet link on this page, should be near where you post a comment.
vs