Convert number to a reversed array of digits in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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 ]
raja
Published on 09-Oct-2020 11:19:46
Advertisements