| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Generates the lyrics for a range of verses in the 99 Bottles of Beer song. |
| 8 | * |
| 9 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 10 | * @param {number} takeDownCount - The number of verses to generate (default: 1) |
| 11 | * @return {string[]} - Array of strings representing the song verses |
| 10 | 12 | */ |
| 11 | 13 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 14 | // Edge Case: Invalid input types |
| 15 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 16 | throw new Error('Invalid input: both parameters must be numbers'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Negative values |
| 20 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 21 | throw new Error('Invalid input: parameters must be non-negative'); |
| 22 | } |
| 23 | |
| 24 | // Edge Case: takeDownCount is 0 |
| 25 | if (takeDownCount === 0) { |
| 26 | return []; |
| 27 | } |
| 28 | |
| 29 | const lines = []; |
| 30 | |
| 31 | for (let i = 0; i < takeDownCount; i++) { |
| 32 | const currentBottles = initialBottlesCount - i; |
| 33 | |
| 34 | // Edge Case: Going below 0 bottles |
| 35 | if (currentBottles < 0) { |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | // Add empty line between verses (except before the first verse) |
| 40 | if (i > 0) { |
| 41 | lines.push(''); |
| 42 | } |
| 43 | |
| 44 | // Split the verse into lines and add them |
| 45 | const verseLines = createVerse(currentBottles).split('\n'); |
| 46 | lines.push(...verseLines); |
| 47 | } |
| 48 | |
| 49 | return lines; |
| 13 | 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Creates a single verse of the 99 Bottles of Beer song. |
| 54 | * |
| 55 | * @param {number} numBottles - The number of bottles for this verse |
| 56 | * @return {string} - The verse as a string |
| 57 | */ |
| 58 | const createVerse = (numBottles) => { |
| 59 | // Edge Case: 0 bottles |
| 60 | if (numBottles === 0) { |
| 61 | return 'No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.'; |
| 62 | } |
| 63 | |
| 64 | // Edge Case: 1 bottle |
| 65 | if (numBottles === 1) { |
| 66 | return '1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.'; |
| 67 | } |
| 68 | |
| 69 | // Edge Case: 2 bottles |
| 70 | if (numBottles === 2) { |
| 71 | return '2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.'; |
| 72 | } |
| 73 | |
| 74 | // General case for 3 or more bottles |
| 75 | return `${numBottles} bottles of beer on the wall, ${numBottles} bottles of beer.\nTake one down and pass it around, ${numBottles - 1} bottles of beer on the wall.`; |
| 76 | }; |
| 77 | |
| 78 | // Handled Edge Cases: Invalid input types, negative values, takeDownCount is 0, going below 0 bottles, 0 bottles, 1 bottle, 2 bottles |