# LeetCode Challenge: Plus One

Time for another LeetCode challenge! This week, I tackled the Plus One challenge. This challenge called to build a function that would receive an array made up of numbers. The function would output an array that adds one to the integer that the original array represents. For example, if the passed-in array is [1, 2, 3], the returned output would be [1, 2, 4]. If the passed-in array is [5, 3, 9], the returned array would be [5, 4, 0].

My initial thoughts are that this specific solution shouldn’t require a loop to do something with each specific element in the array. Since I know from the instructions that there’s only one specific location that needs to be changed in the array, the final number, a loop to check if each number is in the final location won’t be necessary. Through chaining JavaScript methods together, I should be able to come to a solution somewhat easily. Just off the top of my head, I should use the join method to turn the array into a number, then using the split method after adding one to the number to get a solution as the returned value.

Through testing these methods in my Chrome browser’s console, I quickly found that I had forgotten that when an array is joined through the join method, it’s returned as a string instead of an integer. This is easily remedied by using parseInt to convert the string into an integer. After I had the integer from the original array, all I had to do was add one to the number, then turn that integer back into a string and split it apart.

This solution passes the example cases given in the challenge, but when it’s submitted there are still a number of tests failing! Digging into the information, I saw that when the digits array was beyond nineteen digits, the parseInt line was turning every number past that point into zeroes. I had to find an alternative method that would allow me to maintain those numbers beyond the nineteenth digit.

I was eventually able to find the Mozilla Developer Network documentation for BigInt, which is a built-in JavaScript object designed to represent extremely large numbers in JavaScript. Using this object, I can transform the initial string of digits constructed with the join method into a BigInt that maintains the numbers past nineteen digits. Using BigInt in this instance is relatively easy, all I had to do was replace parseInt from line two of my first attempt with BigInt. I also decided to add one to that integer in the integer variable instead of saving a new variable. The ’n’ that you see next to the ‘+ 1’ is just a signifier to show that the number is a BigInt. Since you can’t add an integer to a BigInt, that ’n’ is crucial to prevent the function from throwing an error.

After the integer variable was created, I had to take that new integer and convert it back into a string so it could be converted back into an array, which would be the returned value to finish off the function.

There it is! This function passes all tests, let’s check out the runtime.

Very solid. This function works as O(n) linear time, so it’s an efficient function when it comes to time complexity. The space complexity likely leaves something to be desired, since it’s saving variables of large values of data, but that’s a problem that can be solved another day.