-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcandyDistributor.js
44 lines (36 loc) · 1.1 KB
/
candyDistributor.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
const assert = require('assert');
const candyDistributor = (n, k) => {
const result = {};
let candies = n;
let round = 0;
// Loop until candies are gone
for (let i = 0; candies > 0; i++) {
// Rotate child you're distributing candies to
const child = i % k;
// Number of candies to give
let numCandies = round === 0 ? i + 1 : i + 1 + k;
//If almost out, only give remaining candies
if (numCandies > candies) {
numCandies = candies;
}
// Add candies to child's total
result[child] = result[child] ? result[child] + numCandies : numCandies;
// Subtract from candy total
candies -= numCandies;
// Keep track number of rounds
if (child === k - 1) {
round++;
}
}
// Return result
return Object.values(result);
};
try {
assert.deepEqual(candyDistributor(7, 4), [ 1, 2, 3, 1 ]);
assert.deepEqual(candyDistributor(10, 3), [ 5, 2, 3 ]);
console.log('Tests succeeded');
} catch (e) {
console.log('Tests failed');
console.log('Expected', e.expected);
console.log('Actual', e.actual);
}