The construct:
Math.ceil(averageCost / 10) * 10;
Does a form of rounding. It has the net effect of rounding up to the nearest whole number multiple of 10. So:
11 => 20
20 => 20
21 => 30
11.99 => 20
Math.ceil(averageCost / 10) divides by 10 and rounds up to the nearest whole number (removing all decimal portions and all ones) and then the * 10 brings it back to the same numeric range it was originally in, but without the parts removed by the rounding.
Here's an example showing a number of results:
const nums = [10,11,15,18,19,20,21,10.1,11.99,20.19];
for (let num of nums) {
let result = Math.ceil(num/10) * 10;
console.log("Input: ", num, ", Output: ", result);
}
Why wouldn't we just use Math.ceil? Doesn't it effectively do the same thing as the above? Is there a condition where Math.ceil doesn't work?
This combination rounds up to the nearest whole number multiple of 10 whereas Math.ceil() only rounds up to the nearest whole number. So, the two cases you ask about have different uses. Use the one that accomplishes what you want to accomplish. For a number where the integer portion is already a power of ten such as 10.3, the two would have the same output, but for any other number such as 11.3, the two would not have the same output.
Math.ceil(11.3) === 11
(Math.ceil(11.3 / 10) * 10) === 20
Is there a condition where the
Math.ceil()function in Javascript doesn't remove decimals from the output?
No. There is not. It's whole function is to round up to the nearest whole number (thus removing any decimal fraction from the number).
So I'm at a loss for why I'd see a division by 10 with a subsequent multiplication of 10.
To round up to the nearest whole number multiple of 10.
Answer from jfriend00 on Stack Overflownode.js - Is there a condition where the Math.ceil function in Javascript doesn't remove decimals from the output? - Stack Overflow
javascript - Random Number, Math.floor(...) vs Math.ceil(...) - Stack Overflow
ceil - Need the nearest integer value in javascript - Stack Overflow
Newbie here. Is using Math.ceil(Math.random()) a bad idea?
Videos
as stated in MDN reference about Math.random()
Returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.
Since Math.random can return 0, then Math.ceil(Math.random()*10) could also return 0 and that value is out of your [1..10] range.
About your second question, see Most efficient way to create a zero filled JavaScript array?
Math.floor() is preferred here because of the range of Math.random().
For instance, Math.random() * 10 gives a range of [0, 10). Using Math.floor() you will never get to the value of 10, whereas Math.ceil() may give 0.
Hi, I've been learning JS for a few weeks now, and it's coming along pretty well. Anyways, I've found both the books I'm reading and all the sites I use for tutorials tell me to generate random numbers using the format Math.floor(Math.random()*n+1). Wouldn't it make more sense to just use Math.ceil(Math.random()*n)? I'm only asking because everything teaches the former, but that seems inefficient. Thanks!