DEV Community

duccanhole
duccanhole

Posted on

code every day with me

--DAY 12--

Hi, I am going to make #100DaysOfCode Challenge. Everyday I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
Now let's solve problem today:
-Problem: Plus One
-Detail: here
-My solution (javascript):

var plusOne = function(digits) {
    let i=digits.length-1;
    while(i>=0){
        if(i==0&&digits[0]==9){
            digits[i]=0; digits.unshift(1);
            return digits;
        }
        digits[i]+=1;
        if(digits[i]!=10) return digits;
        else{
            digits[i]=0;
            i--;
        }
    }
    return digits;
};
Enter fullscreen mode Exit fullscreen mode

-->If you have better solution or any question, please comment below. I will appreciate.

Top comments (3)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
sherifhegazy profile image
sherifhegazy • Edited

i've copied it and it's not working in all cases

Collapse
 
frankwisniewski profile image
Frank Wisniewski

Oh sorry. I overlooked the max size of this array. i fix it and post again...