Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Working With Numbers The Random Challenge Solution

wei du
wei du
2,569 Points

is 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?

Milan Pankhania
Milan Pankhania
3,734 Points

just 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.

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);

2 Answers

Milan Pankhania
Milan Pankhania
3,734 Points

So 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?

It 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.