-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDouble Cola.js
More file actions
43 lines (34 loc) · 1010 Bytes
/
Double Cola.js
File metadata and controls
43 lines (34 loc) · 1010 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
32
33
34
35
36
37
38
39
40
41
42
43
var peopleMap = {
1: 'Sheldon',
2: 'Leonard',
3: 'Penny',
4: 'Rajesh',
5: 'Howard'
}
function numbersInSum(n) {
var grade = 0;
var sum = 2 ** grade;
while (sum < n ) {
grade += 1;
sum = sum + 2 ** grade;
}
return sum === n ? {grade: grade, amountOfElems: sum} : {grade: grade - 1, amountOfElems: sum - 2 ** grade};
}
//console.log(numbersInSum(8));
function whoIsNext(names, n) {
var numbersInSumResult = numbersInSum(parseInt(n / names.length));
[order, remainder] = [numbersInSumResult.grade, n - names.length * numbersInSumResult.amountOfElems ];
[orderOfRemainder, remainderOfRemainder] = [parseInt(remainder / 2 ** (order + 1)) , remainder % 2 ** (order + 1)];
return remainderOfRemainder === 0 ? names[orderOfRemainder - 1] : names[orderOfRemainder];
}
console.log(whoIsNext(['Daisuke Aramaki',
'Motoko Kusanagi',
'Batou',
'Togusa',
'Ishikawa',
'Saito',
'Pazu',
'Borma',
'Azuma',
'Yano',
'Proto'], 71));