[프로그래머스] x만큼의 간격이 있는 n개의 숫자

ElenaPark·2021년 3월 10일
0

알고리즘

목록 보기
29/37
post-thumbnail

x만큼의 간격이 있는 n개의 숫자

풀이 1

function solution18(int, nat) {
  const resultArr = [];
  for (let i = 1; i <= nat; i++) {
    resultArr.push(int * i);
  }
  return resultArr;
}

console.log(solution18(2, 5)); // [2,4,6,8,10]
console.log(solution18(4, 3)); // [4,8,12]
console.log(solution18(-4, 2)); // [-4,-8]

풀이 2

function solution19(int, nat) {
  let arr = [];
  let result = 0;
  for (let i = 0; i < nat; i++) {
    result += int;
    arr.push(result);
  }
  return arr;
}


console.log(solution19(2, 5)); // [2,4,6,8,10]
console.log(solution19(4, 3)); // [4,8,12]
console.log(solution19(-4, 2)); // [-4,-8]
profile
Front-end 개발자입니다.

2개의 댓글

comment-user-thumbnail
2021년 3월 14일

열심이구만...멋져

1개의 답글