题目:
Implement a function named generateRange(min, max, step), which takes three arguments and generates a range of integers from min to max, with the step. The first integer is the minimum value, the second is the maximum of the range and the third is the step. (min < max)
Task:
Implement a function named
generateRange(2, 10, 2) // should return array of [2,4,6,8,10]
generateRange(1, 10, 3) // should return array of [1,4,7,10]
Note:
- min < max
- step > 0
- the range does not HAVE to include max (depending on the step)
个人觉得最佳解答:
function generateRange(min, max, step){
let arr = [];
for (let i=min; i<=max; i += step) {
arr.push(i);
}
return arr;
}
自己的解答:
function generateRange(min, max, step){
let arr = [];
let rel = min + step;
let mid = [rel];
while (rel <= max - step) {
rel = rel + step;
mid.push(rel);
}
arr.push(min, ...mid);
return arr;
}