I am trying to create an array that begins at 0.05 and ends at 2.5. I want the values in between the min and max to grow by increments of 0.245.
var min = 0.05;
var max = 2.5;
var increments = ((max - min) / 100);
var arr = [];
the final output should be like this:
[0.05, 0.0745, 0.99, 0.1235, 0.148 ... 2.5]
using ES6/ES2015 spread operator you can do this way:
let min = 0.05,
max = 2.5,
items = 100,
increments = ((max - min) / items);
let result = [...Array(items + 1)].map((x, y) => min + increments * y);
if you need to round your numbers to x number of digits you can do this way:
let result = [...Array(items + 1)].map((x, y) => +(min + increments * y).toFixed(4));
Here is an example of the code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With