Javascript coding exercise

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?

I think you should do a different implementation. Add 1 to the digit in the highest index position if the number is less than 8. If 9 then change 9 to 0 and add one to the previous index number (if it is less than 8) etc. So basically treat it like someone doing a vertical addition calculation in math class.

1 Like

I think that @hbar1st has pointed you to the intention of the challenge.

Yeah I was thinking of a solution like this but I suck at recursion and am not too keen to implement a bunch of for’s and if’s.
I’ll see how it goes thanks

Thanks for the info.

Dont have to use JS but I am primarily practicing the JS for job interviews.

The trick to recursion is to know when to stop. So always code the stop condition first. That is, if you are given an array with a single number, how would you add one to it? If you can write this code to work for 0 to 9 then you can solve the whole thing.

However i imagine just looping backwards through the array will solve this, so no recursion needed.

1 Like

This problem isn’t a bad candidate for recursion, but if I were to solve it recursively it would look very much like the imperative solution discussed here.

Thanks for everyones input, the info from @camperextraordinaire about math operations on large Javascript numbers is really good to know.

I ended up with this solution built from the advice of @hbar1st

var plusOne = function(digits) {
    if(digits[digits.length-1] < 9) {
        digits[digits.length-1] += 1;
        return digits;
    }else{
      let i = digits.length-1;
      while(digits[i] == 9) {
        digits[i] = 0;
        if(i == 0) {
          digits.unshift(1);
          return digits;
        }
        i--;
      }
      digits[i] += 1;
    }
  return digits;
};

Ends up being a lot easier than I thought with a while loop, and much better than my first implementation.

Yeah cheers rando, I need to get better at reading/writing shorthand and ES6 JS syntax.

I had to read the ternary statement in your return like 10 times before I understood it.

@camperextraordinaire came up with a recursive solution. Might be on similar terms you’re thinking.