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 trial

JavaScript JavaScript Functions Arrow Functions Function Challenge Solution

My solution: Please review and give any comments that could make the code better. Thank you.

let randomNum = (upper, lower) => Math.floor(Math.random(lower) * (upper - 1 + 1)) + 1;

When first approaching this challenge I attempted to use a very long code solution and wanted to challenge myself to shorten or simplify the code. I chose the arrow function because it allows for an implicit return (I'm pretty sure that's the right terminology, please correct if wrong.), and it is simpler code. I am also not used to working with the arrow functions so I wanted to give it a go. And it works in my browser. If anyone has any suggestions please let me know. I appreciate your feedback.

CORRECTED CODE

let randomNum = (upper, lower) => Math.floor(Math.random() * (lower - upper + 1)) + upper;

I put upper and lower is the wrong location.

Thanks Simon, It sure does seem inefficient. I corrected that problem. I very much appreciate your feedback! have a great day!

1 Answer

Simon Coates
Simon Coates
8,481 Points

Do random take a parameter? The bit about upper -1 + 1 seems inefficient.

Mod Edit: Changed comment to answer so this suggestion can be voted on.