-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard-challenge.js
43 lines (41 loc) · 1.06 KB
/
card-challenge.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
const deal = range => {
const cards = [];
while (range > 0) {
const rand = Math.round(Math.random());
cards.push(rand);
range--;
}
return cards;
};
const play = range => {
const deck = deal(range);
const faceUp = deck.filter(d => d === 1);
if (faceUp.length % 2 === 0) {
return 'No answer, unwinnable';
} else {
const update = (deck, index) => {
if (deck.every(d => d === '.')) {
return deck;
} else {
if (deck[index] === 1) {
deck[index] = '.';
if (index !== 0) {
const prev = index - 1;
if (deck[prev] !== '.') {
deck[prev] = deck[prev] === 1 ? 0 : 1;
}
}
if (index + 1 !== deck.length) {
const next = index + 1;
if (deck[next] !== '.') {
deck[next] = deck[next] === 1 ? 0 : 1;
}
}
}
const newIndex = index + 1 < deck.length ? index + 1 : 0;
return update(deck, newIndex);
}
};
return `Can be solved ${update(deck, 0)}`;
}
};