Convert number to reversed array of digits

Lee·2022년 6월 10일

Algorithm

목록 보기
11/92
post-thumbnail

❓ Convert number to reversed array of digits

Q. Convert number to reversed array of digits
Given a random non-negative number, you have to return the digits of this number within an array in reverse order.

Example(Input => Output):
348597 => [7,9,5,8,4,3]
0 => [0]

✔ Solution

function digitize(n) {
  return String(n).split("").reverse().map(Number);
}

Check

  • String(num)
    The String global object acts as a constructor for strings and "converts" the given number into string(in this case)
  • The Array.reverse()
    method reverses an array in place
  • The Array.map()
    method creates and returns a new array calling a
    provided function on every array element
profile
Lee

0개의 댓글