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 Loops, Arrays and Objects Simplify Repetitive Tasks with Loops What are Loops?

Silvia Correia
Silvia Correia
747 Points

Purpose of the function in this script

Why do we have to write function randomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; } for the code to work?

1 Answer

Tadeáš Firich
Tadeáš Firich
7,866 Points

Hello Silvia, it is because you must define what exactly is randomNumber if we want to call it. If it don't make sance you can watch this course javascript-basics. And you can also keep on this track beginner-javascript.

Was it helful for you? You can give me some points. :)

Silvia Correia
Silvia Correia
747 Points

Hi Tadeáš! Many thanks for your answer! I was thinking once we write RandomNumber it would have already the implicit code to give a random number between 1 and the number we choose. But of course makes more sense your answer!! Many thanks!