Description
- Given a random non-negative number, you have to return the digits of this number within an array in reverse order.
Solution 01
function numberToReversedArray(n) {
let str = n.toString();
let result = [];
for (let i = str.length - 1; i >= 0; i--) {
result.push(Number(str[i]));
}
return result;
}
numberToReversedArray(1234); // [4, 3, 2, 1]
numberToReversedArray(4321); // [1, 2, 3, 4]
Solution 02
function numberToReversedArray(n) {
return n.toString().split('').reverse().map(i => +i);
}
numberToReversedArray(1234); // [4, 3, 2, 1]
numberToReversedArray(4321); // [1, 2, 3, 4]
Solution 03
function numberToReversedArray(n) {
return n.toString().split('').reverse().map(Number);
}
numberToReversedArray(1234); // [4, 3, 2, 1]
numberToReversedArray(4321); // [1, 2, 3, 4]