I am tasked with creating a function called runOnRange that will populate a new array based on the properties of an object. The object contains three properties: start, end, and step. The goal is to push specific numbers into the array according to these properties and then return the array.
As a beginner, I need to stick to basic techniques like loops and conditionals.
Here are some examples that illustrate what is needed:
runOnRange({start: 10, end: 17, step: 3})
// => 10 // => 13 // => 16
runOnRange({start: -6, end: -4})
// => -6 // => -5 // => -4
runOnRange({start: 12, end: 12})
// nothing should be console.logged in this case!
runOnRange({start: 23, end: 26, step: -1})
// nothing should be console.logged in this case!
runOnRange({start: 26, end: 24, step: -1})
// => 26 // => 25 // => 24
runOnRange({start: 23, end: 26, step: 0})
// nothing should be console.logged in this case!
This is the current code implementation:
function runOnRange (object) {
var arrNew = []
var start = object.start
var end = object.end
var step = object.step
//Case 1: steps between start and end range
if(start + step <= end && start + step >= start) {
for (var i = start; i <= end; i = i + step) {
arrNew[i]=i;
}
}
//Case 2: steps not set, defaulting to increment by one
if (step == undefined) {
step == 1;
if(start + step <= end && start + step >= start) {
for (var i = start; i <= end; i = i + step) {
arrNew[i]=i
}
}
}
return arrNew
}
When running
runOnRange({start: 10, end: 17, step: 3})
the output in the console shows
(17) [empty × 10, 10, empty × 2, 13, empty × 2, 16]
indicating there is at least one error.
Executing
runOnRange({start: -6, end: -4})
results in an empty array, whereas it should increase the steps parameter by one.
Where have I made mistakes?