You are given a large integer represented as an integer array digits, where each digits[i]
is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
My solution:
/**
* @param {number[]} digits
* @return {number[]}
*/
var plusOne = function(digits) {
let num = Number(digits.join('')) + 1
const myFunc = x => Number(x);
digits = Array.from(String(num), myFunc)
return digits
};
console.log(plusOne([1,2,3,5,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7]));
Why does the above code not work given the following argument:
[1,2,3,5,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7]
my output:
[1,NaN,2,3,5,6,7,7,7,7,7,7,7,7,7,7,7,7,NaN,NaN,2,1]
expected output:
[1,2,3,5,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8]