I am trying to make a function called range which takes 3 integer parameters: start, end, and step.
The function should return an array of numbers from start to end counting by step.
The function should return an empty array if given incorrect parameters, such as:
Start, end, or step being undefined. Start being greater than end. Step being negative.
This is my code:
const range = function(start, end, step) {
const result = []
for (let i = start; i <= end; i += step) {
return i || end || step == undefined ? []
: i > end ? []
: step < 0 ? []
: result.push(i)
}
return result;
}
console.log(range(0, 10, 2));
console.log(range(10, 30, 5));
console.log(range(-5, 2, 3));
All of these return an empty array.
I am new to JavaScript, so please...the more you explain, the more you help me.
Thank you.