Problem
We are required to write a JavaScript function that takes in a number. Our function should first reverse the number and then split the reversed number into digits and return that splitted array of digits.
Input
const num = 1234567;
Output
const output = [7, 6, 5, 4, 3, 2, 1];
Because the reverse number is 7654321
Example
Following is the code −
const num = 1234567;
const reverseAndSplit = (num = 1) => {
const numStr = String(num);
const arr = numStr.split('');
arr.reverse();
return arr.map(el => {
return Number(el);
});
};
console.log(reverseAndSplit(num));Output
[7, 6, 5, 4, 3, 2, 1]