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 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!