← Back to challenges

Numbers to Arrays and Vice Versa

JavaScriptHardarraysnumbers

Instructions

Write two functions:

  1. toArray(), which converts a number to an array of its digits.
  2. toNumber(), which converts an array of digits back to its number.

Examples

toArray(235) ➞ [2, 3, 5]

toArray(0) ➞ [0]

toNumber([2, 3, 5]) ➞ 235

toNumber([0]) ➞ 0

Notes

All test cases will be weakly positive numbers: >= 0

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Most Left Digit