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 trialTan Ahmed
1,436 Pointsis my code ok?
function gen(lower, upper){
if (lower >= upper){
var num = Math.floor(Math.random() * (parseInt(lower) - parseInt(upper) + 1)) + 1;
return num;
} else{
var num = Math.floor(Math.random() * (parseInt(upper) - parseInt(lower) + 1)) + 1;
return num;
}
}
alert(gen(5, 10));
1 Answer
Cory Harkins
16,500 PointsHi Tan! Your function is good! My only critique would be to not repeat yourself.
You've declared num twice within your function, you should only declare it once.
function doSomething(a,b){
var declareMeOnce = 0;
if (a >=b) {
declareMeOnce = someNumber;
return declareMeOnce;
} else {
declareMeOnce = someNumber;
return declareMeOnce;
}
}
As for the logic of your code, it looks as though you are adding an extra +1 to the end of your expression, making the result of the expression Floor + 1. Not sure if that was your intention.
Tan Ahmed
1,436 PointsTan Ahmed
1,436 PointsHi Cory, thank you for your response. So can I remove the +1 at the end? and thank you for the good idea, I can remove the extra num var declaration.