ds-algo-study
Version:
Just experimenting with publishing a package
14 lines (13 loc) • 669 B
JavaScript
//Write a range function that takes three arguments, start, end and the "step" value used when building the array.If no step is given, the elements go up by increments of one, corresponding to the old behavior. The function call range(1, 10, 2) should return [1, 3, 5, 7, 9]. Make sure it also works with negative step values so that range(5, 2, -1) produces [5, 4, 3, 2].
function range(start, end, step = start < end ? 1 : -1){
let array= [];
if (step > 0) {
for (let i=start; i<=end; i+=step)
array.push(i)
} else {
for (let i=start; i>=end; i+=step)
array.push(i)
}
return array;
}
console.log(range(0 , -20 , -3));