Need to receive:
digitize(12345) -> [5,4,3,2,1]
I wrote a code:
function digitize(n) {
let arr = Array.from(n + '');
return arr.reverse();
}
console.log(digitize(12345));
Output: [ ‘5’, ‘4’, ‘3’, ‘2’, ‘1’ ]
This is very close, but this is showing an array of strings. How can I get an array of numbers (without the quotes), instead?
>Solution :
You could map (the second parameter of Array.from) with Number.
This approach works only for positive integers which are equal or less than Number.MAX_SAFE_INTEGER (9007199254740991).
function digitize(n) {
return Array
.from(n.toString(), Number)
.reverse();
}
console.log(digitize(12345));