This is the question: https://leetcode.com/problems/plus-one/
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.
This is my code:
var plusOne = function(digits) {
let num = 0;
for(let i = 0; i < digits.length; i++){
num += digits[i] * (10 ** (digits.length-i-1))
}
return (num+1).toString().split("").map(Number)
};
Failed test case:
Input:
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]
Output:
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,0,0,0]
Expected:
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,4]
I realised that the code also works fine as long as digits.length <= 17, for e.g. [1,2,3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7] Once there are 18 numbers in digits for e.g. [1,2,3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8] it stops working and I can't seem to find the bug with this. Can someone please help enlighten me? Thank you!!