We are required to write a JavaScript function that takes in a Number as the only input. The function should simply split the digits of the number and construct and return an array of those digits.
For example −
If the input number is −
const num = 55678;
Then the output should be −
const output = [5, 5, 6, 7, 8];
The only condition is that we cannot convert the Number to String or use any ES6 function over it.
Example
The code for this will be −
const num = 55678;
const numberToArray = (num) => {
const res = [];
while(num){
const last = num % 10;
res.unshift(last);
num = Math.floor(num / 10);
};
return res;
};
console.log(numberToArray(num));Output
And the output in the console will be −
[ 5, 5, 6, 7, 8 ]