Step Between Two Numbers
const stepBetweenA = (min, max, steps) =>
Array(steps).fill(0).reduce((prev, curr, i) => {
prev.push(min + ((max - min) / (steps - 1)) * i)
return prev
}, [])
const stepBetweenB = (min, max, steps) => {
steps -= 1
const diff = (max - min) / steps
const result = [min]
for (let i = 0; i < steps; i++) {
result.push(min += diff)
}
return result
}
console.log('a', stepBetweenA(10, 110, 4))
console.log('b', stepBetweenB(10, 110, 4))
const ITER = 10000
console.time('a')
for (let i = 0; i < ITER; i++) {
stepBetweenA(10, 110, 4)
}
console.timeEnd('a')
console.time('b')
for (let i = 0; i < ITER; i++) {
stepBetweenB(10, 110, 4)
}
console.timeEnd('b')
Two messy implementations for stepping between two numbers… I am not sure it’s possible to make console.time
work well with the snippet zone quick editor – so if you want to see the times… open your console.