I'm struggling to find the right approach here.
Can someone help me convert the number -800 into [-8, 0, 0]?
To start, I turned the number into a string and used the map function to create an array:
const number = -800;
const numberString = number.toString();
const arrayString = numberString.split('').map((x) => +x);
console.log(arrayString)
The current result is [ NaN, 8, 0, 0 ]
How can I change the NaN
and first index of 8
to make it -8
, while keeping the other indexes unchanged? This way, it will be [-8, 0, 0]
Any assistance would be greatly appreciated!
Thank you.