DEV Community

Discussion on: Daily Challenge #189 - Convert Number into Reversed Array

Collapse
 
qm3ster profile image
Mihail Malo

Whoops, looks like someone didn't specify the base of the number :o
How naughty!

function convertNtA(num, base = 10) {
  const len = Math.ceil(Math.log(num) / Math.log(base)),
    arr = Array(len)
  for (let i = 0; i < len; i++) {
    arr[i] = num % base
    num = (num / base) | 0
  }
  return arr
}
convertNtA(348597)
convertNtA(0x348597, 16)