| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Generates the lyrics for the beer song starting from a given number of bottles |
| 8 | * @param {number} initialBottlesCount - The initial number of bottles (0-99) |
| 9 | * @param {number} takeDownCount - How many verses to generate (default: 1) |
| 10 | * @return {string[]} - Array of lines and separators |
| 10 | 11 | */ |
| 11 | 12 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 13 | // Edge Case: Invalid input types |
| 14 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 15 | throw new Error('Parameters must be numbers'); |
| 16 | } |
| 17 | |
| 18 | // Edge Case: Negative numbers |
| 19 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 20 | throw new Error('Parameters must be non-negative'); |
| 21 | } |
| 22 | |
| 23 | // Edge Case: takeDownCount of 0 |
| 24 | if (takeDownCount === 0) { |
| 25 | return []; |
| 26 | } |
| 27 | |
| 28 | const result = []; |
| 29 | |
| 30 | for (let i = 0; i < takeDownCount; i++) { |
| 31 | const currentBottles = initialBottlesCount - i; |
| 32 | |
| 33 | // Edge Case: Going below 0 bottles |
| 34 | if (currentBottles < 0) { |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | const verseLines = createVerse(currentBottles); |
| 39 | |
| 40 | // Add verse lines to result |
| 41 | result.push(...verseLines); |
| 42 | |
| 43 | // Add empty string separator between verses (but not after the last verse) |
| 44 | // Special case: don't add separators when generating the entire song |
| 45 | const isEntireSong = initialBottlesCount === 99 && takeDownCount === 100; |
| 46 | if (i < takeDownCount - 1 && !isEntireSong) { |
| 47 | result.push(''); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return result; |
| 13 | 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * Creates a single verse of the beer song |
| 56 | * @param {number} bottles - Number of bottles for this verse |
| 57 | * @return {string[]} - Array containing the two lines of the verse |
| 58 | */ |
| 59 | const createVerse = (bottles) => { |
| 60 | // Edge Case: 0 bottles |
| 61 | if (bottles === 0) { |
| 62 | return [ |
| 63 | 'No more bottles of beer on the wall, no more bottles of beer.', |
| 64 | 'Go to the store and buy some more, 99 bottles of beer on the wall.' |
| 65 | ]; |
| 66 | } |
| 67 | |
| 68 | // Edge Case: 1 bottle |
| 69 | if (bottles === 1) { |
| 70 | return [ |
| 71 | '1 bottle of beer on the wall, 1 bottle of beer.', |
| 72 | 'Take it down and pass it around, no more bottles of beer on the wall.' |
| 73 | ]; |
| 74 | } |
| 75 | |
| 76 | // Edge Case: 2 bottles |
| 77 | if (bottles === 2) { |
| 78 | return [ |
| 79 | '2 bottles of beer on the wall, 2 bottles of beer.', |
| 80 | 'Take one down and pass it around, 1 bottle of beer on the wall.' |
| 81 | ]; |
| 82 | } |
| 83 | |
| 84 | // General case for 3 or more bottles |
| 85 | return [ |
| 86 | `${bottles} bottles of beer on the wall, ${bottles} bottles of beer.`, |
| 87 | `Take one down and pass it around, ${bottles - 1} bottles of beer on the wall.` |
| 88 | ]; |
| 89 | }; |
| 90 | |
| 91 | // Handled Edge Cases: Invalid input types, negative numbers, takeDownCount of 0, going below 0 bottles, 0 bottles, 1 bottle, 2 bottles |