I am doing this coding exercise on leetcode.com and am getting unexpected output
https://leetcode.com/problems/plus-one/description/
The function is supposed to receive an array of single numbers, but you treat the array as a full number and add 1.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
My code
var plusOne = function(digits) {
let str = ''
for(let num of digits) {
str += num
}
let num = parseInt(str);
num++;
let strArr = [...num.toString()]
let finArr = []
let temp = 0;
for(let item of strArr) {
temp = parseInt(item)
finArr.push(temp)
}
return finArr
};
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]
In this test case the final 3 indexes are getting pushed as 0
's and I cannot figure out why.
Any ideas?