While working on a problem in leet code, I managed to come up with a solution that passed all test cases except for one. The input for that particular test case is an array = [6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]. To solve the issue, I needed to convert the array into a number, add 1 to the entire number, and then convert it back to an array format with the result. In my solution, one step before the final statement, I used parseInt("6145390195186705543")+1, then converted it to a string, split it, and finally converted it back to a number.
However, during the parseInt() process, the built-in method was unable to convert after the 15th digit. The output showed as [6145390195186705000], with zeros being added after the 15 digits. Does anyone have any suggestions on how to convert a string of numbers longer than 16 characters to a Number?
P.S: I tried using the bigInt() method, which technically should work, but for this particular problem, bigInt() is not functioning properly and the output isn't correct.
var plusOne = function(digits) {
let y = digits.map(String);
let z = ''
for (let i = 0; i < y.length; i++) {
z += y[i];
}
let a = (parseInt(z) + 1).toString().split('')
return a.map(Number)
};