알고리즘 31 - Count the Monkeys!

jabae·2021년 10월 25일
0

알고리즘

목록 보기
31/97

Q.

You take your son to the forest to see the monkeys. You know that there are a certain number there (n), but your son is too young to just appreciate the full number, he has to start counting them from 1.

As a good parent, you will sit and count with him. Given the number (n), populate an array with all numbers up to and including that number, but excluding zero.

For example:

monkeyCount(10) // --> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
monkeyCount(1) // --> [1]

A)

function monkeyCount(n) {
  return new Array(n).fill().map((result, i) => i + 1)
}

or

function monkeyCount(n) {
  let buf = [];
  
  for (let i = 1; i <= n; i++) {
    buf = buf.concat(i);
  }
  return buf;
}
profile
it's me!:)

0개의 댓글