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