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