Convert Number to a Reversed Array of Digits in JavaScript



Given a non-negative integer, we are required to write a function that returns an array containing a list of independent digits in reverse order.

For example:

348597 => The correct solution should be [7,9,5,8,4,3]

The code for this will be −

const num = 348597;
const reverseArrify = num => {
   const numArr = String(num).split('');
   const reversed = [];
   for(let i = numArr.length - 1; i >= 0; i--){
      reversed[i] = +numArr.shift();
   };
   return reversed;
};
console.log(reverseArrify(num));

Following is the output on console −

[ 7, 9, 5, 8, 4, 3 ]
Updated on: 2020-10-09T11:20:15+05:30

155 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements