알고리즘 70 - Powers of 2

tamagoyakii·2021년 10월 22일
0

알고리즘

목록 보기
70/89

Q.

Complete the function that takes a non-negative integer n as input, and returns a list of all the powers of 2 with the exponent ranging from 0 to n (inclusive).

Examples
n = 0 ==> [1] # [2^0]
n = 1 ==> [1, 2] # [2^0, 2^1]
n = 2 ==> [1, 2, 4] # [2^0, 2^1, 2^2]

A)

function powersOfTwo(n){
  let ret = [];
  for (let i = 0; i <= n; i++) ret.push(Math.pow(2, i))
  return ret;
}

0개의 댓글