| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 7 | * Recites verses of the '99 Bottles of Beer' song. |
| 8 | * |
| 9 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 10 | * @param {number} takeDownCount - How many verses to recite (default: 1) |
| 11 | * @return {string[]} - Array of lines (verses separated by empty strings) |
| 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('Both parameters must be numbers'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: Negative numbers |
| 20 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 21 | throw new Error('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 - wrap around to 99 |
| 35 | if (currentBottles < 0) { |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | const verseLines = createVerse(currentBottles); |
| 40 | |
| 41 | // Add verse lines to result |
| 42 | lines.push(...verseLines); |
| 43 | |
| 44 | // Add empty string between verses (but not after the last verse) |
| 45 | if (i < takeDownCount - 1) { |
| 46 | lines.push(''); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return lines; |
| 13 | 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * Creates a single verse of the beer song based on the number of bottles. |
| 55 | * |
| 56 | * @param {number} bottles - Number of bottles for this verse |
| 57 | * @return {string[]} - The verse as array of lines |
| 58 | */ |
| 59 | const createVerse = (bottles) => { |
| 60 | // Edge Case: No bottles left |
| 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: One bottle left |
| 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: Two bottles left |
| 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 | // Standard case: More than 2 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 is 0, going below 0 bottles, no bottles left, one bottle left, two bottles left |