-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
pirate-code.js
68 lines (58 loc) · 1.62 KB
/
pirate-code.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function amaroPlan(pirateNum){
// a place to store the gold
const gold = [];
const totalGold = pirateNum * 20;
// iterate up to the number of pirates
for (let i = 0; i < pirateNum; i++) {
// if initial index
if (i === 0) {
// push total gold - (number of pirates - 1) / 2 - drop remainder
const amaroGold = totalGold - Math.floor((pirateNum - 1) / 2);
gold.push(amaroGold);
} else if (i % 2 === 0) {
// if index is even push 1
gold.push(1);
} else {
// else push 0
gold.push(0);
}
}
return gold;
}
function amaroPlan(pirateNum){
const gold = [];
const totalGold = pirateNum * 20;
const amaroGold = totalGold - Math.floor((pirateNum - 1) / 2);
gold.push(amaroGold);
for (let i = 1; i < pirateNum; i++) {
gold.push((i + 1) % 2);
}
return gold;
}
function amaroPlan(pirateNum) {
const gold = [];
let remainingGold = pirateNum * 20;
for (let i = pirateNum - 1; i >= 1; i--) {
const ammount = (i + 1) % 2;
remainingGold -= ammount;
gold[i] = ammount;
}
gold[0] = remainingGold;
return gold;
}
function amaroPlan(pirateNum) {
return Array.from({ length: pirateNum }, (_, i) => {
if (i === 0) {
return pirateNum * 20 - Math.floor((pirateNum - 1) / 2);
} else {
return (i + 1) % 2;
}
});
}
function amaroPlan(pirateNum) {
return Array.from({ length: pirateNum }, (_, i) => i === 0 ? pirateNum * 20 - Math.floor((pirateNum - 1) / 2) : (i + 1) % 2);
}
console.log(amaroPlan(2), [40, 0]);
console.log(amaroPlan(3), [59, 0, 1]);
console.log(amaroPlan(4), [79, 0, 1, 0]);
console.log(amaroPlan(5), [98, 0, 1, 0, 1]);