DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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 on

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...

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

β­οΈπŸŽ€ JavaScript Visualized: Promises & Async/Await

async await