Leet code problem passes with no curly braces but fails with them ...why?

I’ve been stuck on this question for like an hour or more trying to figure out what was wrong, finally broke down and looked up an answer. The answer I found was identical to mine except they didn’t use curly braces around the last if statement. When I deleted my curly braces it passed. Why does this happen? Can someone explain the difference or direct me to some documentation to really explain this.

var distributeCandies = function(candies, num_people) {
    
    
    const a = new Array(num_people).fill(0);
    
    let i = 0;
    let x = 1;
    
    while(candies >0){
        
        a[i]+= x;
        candies -= x;
        
        
        if(candies <0){
         
            a[i] += candies;
            break;
        }
        
        i++;
        
        if(i === num_people)// if i put curly braces here then it fails 
            i = 0;
            x++;
        
        
    }
   return a;
};

link to the problem:
Distribute Candies to People - LeetCode

The difference is if x++ is guarded by the if statement or not.

:rofl: wow … i was so confused but that is 100% it, i was accidently putting it in the curly braces. Just when you think you know something you get stuck on the silliest stuff

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.