-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkata1ex.js
More file actions
31 lines (25 loc) · 732 Bytes
/
kata1ex.js
File metadata and controls
31 lines (25 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function distribute(m, n) {
// if n is zero or less
// return empty array
if (n <= 0) return [];
// need an n length array to store the candies
// filled with zeros
const candies = Array.from({ length: n}).fill(0);
if (m <= 0) return candies;
const fillCandies = () => {
// iterate up to number of children (n)
for (let i = 0; i < candies.length; i++) {
// increment the current value at the current index by 1
candies[i]++;
// decrement the number of candies left
m--;
// if numbers of candies is zero
// break
if (m === 0) break;
}
};
while(m > 0) {
fillCandies();
};
return candies;
}