| 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 | * Generates the lyrics for the '99 Bottles of Beer' song. |
| 3 | * Handles special cases for grammar and phrasing at key transition points. |
| 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 strings representing the song verses |
| 12 | * |
| 13 | * Edge Cases Handled: |
| 14 | * - When initialBottlesCount is 0 |
| 15 | * - When initialBottlesCount is 1 |
| 16 | * - When initialBottlesCount is 2 |
| 17 | * - Proper pluralization of "bottle" vs "bottles" |
| 18 | * - Special phrasing for "no more bottles" |
| 19 | * - Special action phrase for 1 bottle ("Take it down") vs others ("Take one down") |
| 10 | 20 | */ |
| 11 | 21 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 22 | // Edge Case: Validate input parameters |
| 23 | if (initialBottlesCount < 0 || initialBottlesCount > 99) { |
| 24 | throw new Error('initialBottlesCount must be between 0 and 99'); |
| 25 | } |
| 26 | |
| 27 | if (takeDownCount < 0) { |
| 28 | throw new Error('takeDownCount must be non-negative'); |
| 29 | } |
| 30 | |
| 31 | const verses = []; |
| 32 | |
| 33 | // Edge Case: Handle when no verses are requested |
| 34 | if (takeDownCount === 0) { |
| 35 | return []; |
| 36 | } |
| 37 | |
| 38 | for (let i = 0; i < takeDownCount; i++) { |
| 39 | const currentBottles = initialBottlesCount - i; |
| 40 | const nextBottles = currentBottles - 1; |
| 41 | |
| 42 | // Edge Case: Handle when we've gone below 0 bottles |
| 43 | if (currentBottles < 0) { |
| 44 | break; |
| 45 | } |
| 46 | |
| 47 | // Generate the first line of the verse |
| 48 | let firstLine = ''; |
| 49 | if (currentBottles === 0) { |
| 50 | firstLine = 'No more bottles of beer on the wall, no more bottles of beer.'; |
| 51 | } else { |
| 52 | const bottleText = currentBottles === 1 ? 'bottle' : 'bottles'; |
| 53 | firstLine = `${currentBottles} ${bottleText} of beer on the wall, ${currentBottles} ${bottleText} of beer.`; |
| 54 | } |
| 55 | |
| 56 | // Generate the second line of the verse |
| 57 | let secondLine = ''; |
| 58 | if (currentBottles === 0) { |
| 59 | secondLine = 'Go to the store and buy some more, 99 bottles of beer on the wall.'; |
| 60 | } else if (currentBottles === 1) { |
| 61 | secondLine = 'Take it down and pass it around, no more bottles of beer on the wall.'; |
| 62 | } else { |
| 63 | const nextBottleText = nextBottles === 1 ? 'bottle' : 'bottles'; |
| 64 | if (nextBottles === 0) { |
| 65 | secondLine = 'Take one down and pass it around, no more bottles of beer on the wall.'; |
| 66 | } else { |
| 67 | secondLine = `Take one down and pass it around, ${nextBottles} ${nextBottleText} of beer on the wall.`; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | verses.push(firstLine); |
| 72 | verses.push(secondLine); |
| 73 | |
| 74 | // Edge Case: Add empty string for spacing between verses (except for the last verse) |
| 75 | if (i < takeDownCount - 1) { |
| 76 | verses.push(''); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return verses; |
| 13 | 81 | }; |
| 82 | |
| 83 | // Handled Edge Cases: |
| 84 | // - Invalid initialBottlesCount values (< 0 or > 99) |
| 85 | // - Negative takeDownCount |
| 86 | // - Zero takeDownCount (no verses requested) |
| 87 | // - Going below 0 bottles during iteration |
| 88 | // - Special case for 0 bottles ("No more bottles...") |
| 89 | // - Special case for 1 bottle ("Take it down" instead of "Take one down") |
| 90 | // - Proper pluralization of "bottle" vs "bottles" |
| 91 | // - Special phrasing for "no more bottles" in second line |
| 92 | // - Adding empty lines between verses |