| 1 | | // |
| 2 | | // This is only a SKELETON file for the 'Beer Song' exercise. It's been provided as a |
| 3 | | // convenience to get you started writing code faster. |
| 4 | | // |
| 1 | /* |
| 2 | * |
| 3 | * This is only a SKELETON file for the 'Beer Song' exercise. It's been provided as a |
| 4 | * convenience to get you started writing code faster. |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Helper function to get the correct bottle phrase based on count |
| 10 | * @param {number} count |
| 11 | * @return {string} |
| 12 | */ |
| 13 | const getBottlePhrase = (count) => { |
| 14 | if (count === 0) { |
| 15 | return 'no more bottles'; |
| 16 | } else if (count === 1) { |
| 17 | return '1 bottle'; |
| 18 | } else { |
| 19 | return `${count} bottles`; |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | /** |
| 24 | * Helper function to get the correct action phrase |
| 25 | * @param {number} count |
| 26 | * @return {string} |
| 27 | */ |
| 28 | const getActionPhrase = (count) => { |
| 29 | if (count === 0) { |
| 30 | return 'Go to the store and buy some more'; |
| 31 | } else if (count === 1) { |
| 32 | return 'Take it down and pass it around'; |
| 33 | } else { |
| 34 | return 'Take one down and pass it around'; |
| 35 | } |
| 36 | }; |
| 5 | 37 | |
| 6 | 38 | /** |
| 7 | 39 | * @param {number} initialBottlesCount |
| 9 | 41 | * @return {string[]} |
| 10 | 42 | */ |
| 11 | 43 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 44 | // Edge Case: Invalid input types |
| 45 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 46 | throw new Error('Both parameters must be numbers'); |
| 47 | } |
| 48 | |
| 49 | // Edge Case: Negative initial bottles count |
| 50 | if (initialBottlesCount < 0) { |
| 51 | throw new Error('Initial bottles count cannot be negative'); |
| 52 | } |
| 53 | |
| 54 | // Edge Case: Negative take down count |
| 55 | if (takeDownCount < 0) { |
| 56 | throw new Error('Take down count cannot be negative'); |
| 57 | } |
| 58 | |
| 59 | // Edge Case: Take down count greater than initial bottles count |
| 60 | if (takeDownCount > initialBottlesCount + 1) { |
| 61 | throw new Error('Cannot take down more bottles than available'); |
| 62 | } |
| 63 | |
| 64 | const result = []; |
| 65 | |
| 66 | for (let i = 0; i < takeDownCount; i++) { |
| 67 | const currentBottles = initialBottlesCount - i; |
| 68 | const nextBottles = currentBottles - 1; |
| 69 | |
| 70 | // Edge Case: When we reach the final verse (0 bottles) |
| 71 | if (currentBottles === 0) { |
| 72 | result.push( |
| 73 | `${getBottlePhrase(currentBottles).charAt(0).toUpperCase() + getBottlePhrase(currentBottles).slice(1)} of beer on the wall, ${getBottlePhrase(currentBottles)} of beer.`, |
| 74 | `${getActionPhrase(currentBottles)}, ${getBottlePhrase(99)} of beer on the wall.` |
| 75 | ); |
| 76 | } else { |
| 77 | result.push( |
| 78 | `${getBottlePhrase(currentBottles)} of beer on the wall, ${getBottlePhrase(currentBottles)} of beer.`, |
| 79 | `${getActionPhrase(currentBottles)}, ${getBottlePhrase(nextBottles === -1 ? 99 : nextBottles)} of beer on the wall.` |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | // Edge Case: Add empty string after each verse except the last one |
| 84 | if (i < takeDownCount - 1) { |
| 85 | result.push(''); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return result; |
| 13 | 90 | }; |
| 91 | |
| 92 | // Handled Edge Cases: Invalid input types, negative initial bottles count, negative take down count, take down count greater than initial bottles count, final verse (0 bottles), empty string after verses |