-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise2++.js
52 lines (48 loc) · 1.43 KB
/
exercise2++.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
//initialice a counter for every card in a suite;
let emptySuit = {
J: 0,
Q: 0,
K: 0,
A: 0,
};
//fill empty
for (let i = 2; i <= 10; i++) {
emptySuit[i] = 0;
}
//create a 4 suite (deck-like) group of counters
emptyDeckGroup = {
hearts: { ...emptySuit },
clubs: { ...emptySuit },
diamonds: { ...emptySuit },
spades: { ...emptySuit },
};
function fullDecks(deck) {
let min = Infinity; //maximum possible of cards in a suite
for (const suit_key in deck) {
if (Object.hasOwnProperty.call(deck, suit_key)) {
const suit = deck[suit_key];
//iterates over the card counters
for (const card_key in suit) {
if (Object.hasOwnProperty.call(suit, card_key)) {
const element = suit[card_key];
//if current counter is less than minimum, minimum = current counter
if (element < min) min = element;
}
}
}
}
//return minimum counter, which also indicates the amount of full decks
return min;
}
//counts all the similar cards grouped by type and suit returning the amount of full decks
function orderCards(cards) {
//initialize a deckGroup with all card counters on 0;
const deckGroup = JSON.parse(JSON.stringify(emptyDeckGroup));
//iterate over the cards
cards.forEach((card) => {
//increment counter corresponding to the current card's suit and value
deckGroup[card.suit][card.value]++;
});
return fullDecks(deckGroup);
}
module.exports = orderCards;