Update number-to-array-of-digits.md

This commit is contained in:
Angelos Chalaris
2017-12-14 22:53:42 +02:00
committed by GitHub
parent f827f0d109
commit 69b3e2b97f

View File

@ -1,14 +1,9 @@
### Number to array of digits
Convert the number to a string, split the string using `split()`.
use `parseInt()` to convert every element back to integer.
Convert the number to a string, use `split()` to convert build an array.
Use `Array.map()` and `parseInt()` to transform each value to an integer.
```js
const num2array = (n) =>{
return (''+n).split('').map((i) =>{
return parseInt(i);
})
}
// num2array(2334) -> [2, 3, 3, 4]
```
const digitize = n => (''+n).split('').map(i => parseInt(i));
// digitize(2334) -> [2, 3, 3, 4]
```