-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathdouble-cola.js
More file actions
31 lines (29 loc) · 870 Bytes
/
double-cola.js
File metadata and controls
31 lines (29 loc) · 870 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 whoIsNext(names, r){
// this fails for really large numbers...
// a place to store the current drink/count
let count = 1;
// iterate up to r
while (count < r) {
// take first value in array and push it twice to the back of the array
// remove the first value
const currentPerson = names.shift();
names.push(currentPerson, currentPerson);
// increment drink
count++;
}
// return first person in the array
return names[0];
}
function whoIsNext(names, n){
while (n > names.length) {
n -= names.length - 1;
n *= 0.5;
n = Math.floor(n);
}
return names[n - 1];
}
const names = ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"];
console.log(whoIsNext(names, 1), "Sheldon");
console.log(whoIsNext(names, 20));
console.log(whoIsNext(names, 52), "Penny");
console.log(whoIsNext(names, 7230702951), "Leonard");