Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function countCards(input) {
- let players = new Map();
- for (let string of input) {
- let parts = string.split(': ');
- let playerName = parts.shift();
- let cards = parts[0].split(', ');
- if (players.has(playerName)) {
- let ownedCards = players.get(playerName);
- cards.push(...ownedCards);
- }
- players.set(playerName, cards);
- }
- for (let playerName of players.keys()) {
- let cardsToBeFiltered = players.get(playerName);
- let filteredCards = cardsToBeFiltered.filter((el, i) => {
- return cardsToBeFiltered.indexOf(el) === i
- });
- players.set(playerName, filteredCards);
- }
- let power = new Map([['J', 11], ['Q', 12], ['K', 13], ['A', 14]]);
- let type = new Map([['S', 4], ['H', 3], ['D', 2], ['C', 1]]);
- for (let playerName of players.keys()) {
- let allCards = players.get(playerName);
- let countOfCards = allCards
- .map(a => {
- if (isNaN(+a[0])) {
- return power.get(a[0]) * type.get(a[1]);
- } else {
- if (a.length === 2) {
- return +a[0] * type.get(a[1]);
- } else {
- return 10 * type.get(a[2]);
- }
- }
- })
- .reduce((a, b) => a + b);
- players.set(playerName, countOfCards);
- }
- for (let [playerName, cardsCount] of players.entries()) {
- console.log(`${playerName}: ${cardsCount}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement