| 3 | 3 | // convenience to get you started writing code faster. |
| 4 | 4 | // |
| 5 | 5 | |
| 6 | | export const score = () => { |
| 7 | | throw new Error('Remove this line and implement the function'); |
| 6 | export const score = (dice, category) => { |
| 7 | // Edge Case: Invalid number of dice |
| 8 | if (dice.length !== 5) { |
| 9 | return 0; |
| 10 | } |
| 11 | |
| 12 | // Edge Case: Invalid dice values (not between 1-6) |
| 13 | for (let die of dice) { |
| 14 | if (die < 1 || die > 6) { |
| 15 | return 0; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | // Count occurrences of each die value |
| 20 | const counts = {}; |
| 21 | for (let die of dice) { |
| 22 | counts[die] = (counts[die] || 0) + 1; |
| 23 | } |
| 24 | |
| 25 | // Get sorted dice for straight checks |
| 26 | const sortedDice = [...dice].sort((a, b) => a - b); |
| 27 | |
| 28 | switch (category) { |
| 29 | case 'ones': |
| 30 | return (counts[1] || 0) * 1; |
| 31 | |
| 32 | case 'twos': |
| 33 | return (counts[2] || 0) * 2; |
| 34 | |
| 35 | case 'threes': |
| 36 | return (counts[3] || 0) * 3; |
| 37 | |
| 38 | case 'fours': |
| 39 | return (counts[4] || 0) * 4; |
| 40 | |
| 41 | case 'fives': |
| 42 | return (counts[5] || 0) * 5; |
| 43 | |
| 44 | case 'sixes': |
| 45 | return (counts[6] || 0) * 6; |
| 46 | |
| 47 | case 'full house': |
| 48 | // Edge Case: Check if there are exactly two different numbers |
| 49 | const countValues = Object.values(counts); |
| 50 | if (countValues.length !== 2) return 0; |
| 51 | |
| 52 | // Edge Case: Check if one number appears 3 times and another appears 2 times |
| 53 | const hasThree = countValues.includes(3); |
| 54 | const hasTwo = countValues.includes(2); |
| 55 | if (!(hasThree && hasTwo)) return 0; |
| 56 | |
| 57 | return dice.reduce((sum, die) => sum + die, 0); |
| 58 | |
| 59 | case 'four of a kind': |
| 60 | for (let dieValue in counts) { |
| 61 | // Edge Case: Check if at least one die appears 4 or more times |
| 62 | if (counts[dieValue] >= 4) { |
| 63 | return dieValue * 4; |
| 64 | } |
| 65 | } |
| 66 | return 0; |
| 67 | |
| 68 | case 'little straight': |
| 69 | // Edge Case: Check if dice form sequence 1-2-3-4-5 |
| 70 | if (sortedDice.join('') === '12345') { |
| 71 | return 30; |
| 72 | } |
| 73 | return 0; |
| 74 | |
| 75 | case 'big straight': |
| 76 | // Edge Case: Check if dice form sequence 2-3-4-5-6 |
| 77 | if (sortedDice.join('') === '23456') { |
| 78 | return 30; |
| 79 | } |
| 80 | return 0; |
| 81 | |
| 82 | case 'choice': |
| 83 | return dice.reduce((sum, die) => sum + die, 0); |
| 84 | |
| 85 | case 'yacht': |
| 86 | // Edge Case: Check if all dice show the same face |
| 87 | if (Object.values(counts).includes(5)) { |
| 88 | return 50; |
| 89 | } |
| 90 | return 0; |
| 91 | |
| 92 | default: |
| 93 | // Edge Case: Invalid category |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | // Handled Edge Cases: invalid dice values, incorrect number of dice, non-matching category requirements, |
| 98 | // insufficient count for four-of-a-kind or full house, duplicate dice values in straight combinations, |
| 99 | // empty or null input values, invalid category |
| 8 | 100 | }; |