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

Math.floor vs Math.random & When is each Loop more suited for different tasks.

I was wondering what the difference was between math.floor and math.random

And with loops we have forEach, while, do, and for. I don't see why we don't just use for. So far I don't see much difference between them and definitely wouldn't know in what projects which loop is more appropriate.

2 Answers

Math.floor/ceil and Math.random are two totally different functions:

  • Math.floor rounds a number, variable, or result down.
  • Likewise, Math.ceil (for ceiling) rounds a number, variable, or result up.
  • Math.random generates a random number that is greater than 0, but less than 1, including several decimal places.

So, in order to get a random integer, you need to use these functions in combination.

For instance, to generate a random number between 1 and 100 (inclusive), you might type Math.floor((Math.random() * 100) + 1);

JavaScript math works from the inner set of parentheses outwards. What's happening here is:

  • We generate a fractional number with Math.random, and multiply it by 100
  • Because a very small fractional number would be very close to zero, even if multiplied by 100, we then add 1 to this number to bump it up above zero; otherwise, Math.floor would round it down to zero.
  • And then Math.floor rounds off the result of those two operations, removing any decimal places, leaving us with a nice clean integer.

Regarding loop types, yes, there's more than one way to implement them, and they are designed for different use cases.

  • the for loop is the simplest. It iterates until a condition evaluates to false. Usually, the use case of "go through the list until you run out of things to do."
  • the while loop executes a loop as long as a certain condition continues to be true. For instance, "keep the game animation going as long as the player's ship count is above zero."
  • forEach is basically a way of saying "pass each of this array's elements through this function," without skipping any, with a bit less code than a traditional for loop. It's a shortcut, and doesn't really do anything a for loop couldn't do, as far as I know, but it's nice to have.
  • do-while automatically executes the loop at least once. This is important for use cases where you need it to return at least one result, not an empty or undefined value. for and while loops, by contrast, can evaluate to a condition where the code inside never executes, so this avoids that problem.

Thanks, for the kind response. very helpful

My pleasure. Most of this is covered in the JavaScript Basics course (except forEach) - always worth a rewatch if the basics don't make sense initially. The Mozilla Developer Network (MDN) also has an extensive and free reference online with code samples in context. Happy coding!