알고리즘 68 - Generate range of integers

tamagoyakii·2021년 10월 22일
0

알고리즘

목록 보기
68/89

Q.

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)

A)

function generateRange(min, max, step){
  let ret = [];
  for (let i = min; i <= max; i += step)
    ret = ret.concat([i]);
  return ret;
}

0개의 댓글