| 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 | 'use strict'; |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 4 | * Generates the lyrics for a range of verses in the 99 Bottles of Beer song. |
| 5 | * |
| 6 | * @param {number} initialBottlesCount - The starting number of bottles (0-99). |
| 7 | * @param {number} takeDownCount - The number of verses to generate (default: 1). |
| 8 | * @returns {string[]} An array of strings representing the verses. |
| 9 | * |
| 10 | * @example |
| 11 | * recite(10, 2) // Returns verses for 10 and 9 bottles |
| 12 | * recite(0) // Returns the final verse |
| 10 | 13 | */ |
| 11 | 14 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 15 | // Edge Case: Handle invalid inputs |
| 16 | if (initialBottlesCount < 0 || initialBottlesCount > 99) { |
| 17 | throw new Error('Initial bottle count must be between 0 and 99'); |
| 18 | } |
| 19 | |
| 20 | if (takeDownCount < 0) { |
| 21 | throw new Error('Take down count must be non-negative'); |
| 22 | } |
| 23 | |
| 24 | const verses = []; |
| 25 | |
| 26 | for (let i = 0; i < takeDownCount; i++) { |
| 27 | const currentBottles = initialBottlesCount - i; |
| 28 | |
| 29 | // Edge Case: Prevent negative bottle counts |
| 30 | if (currentBottles < 0) break; |
| 31 | |
| 32 | verses.push(...generateVerse(currentBottles)); |
| 33 | |
| 34 | // Edge Case: Add empty string after each verse except the last one when multiple verses are requested |
| 35 | if (takeDownCount > 1 && i < takeDownCount - 1) { |
| 36 | verses.push(''); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | return verses; |
| 13 | 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * Generates a single verse of the 99 Bottles of Beer song. |
| 45 | * |
| 46 | * @param {number} bottles - The number of bottles for this verse. |
| 47 | * @returns {string[]} An array containing the lines of the verse. |
| 48 | */ |
| 49 | const generateVerse = (bottles) => { |
| 50 | // Edge Case: Handle the final verse (0 bottles) |
| 51 | if (bottles === 0) { |
| 52 | return [ |
| 53 | 'No more bottles of beer on the wall, no more bottles of beer.', |
| 54 | 'Go to the store and buy some more, 99 bottles of beer on the wall.' |
| 55 | ]; |
| 56 | } |
| 57 | |
| 58 | // Edge Case: Handle verse with 1 bottle |
| 59 | if (bottles === 1) { |
| 60 | return [ |
| 61 | '1 bottle of beer on the wall, 1 bottle of beer.', |
| 62 | 'Take it down and pass it around, no more bottles of beer on the wall.' |
| 63 | ]; |
| 64 | } |
| 65 | |
| 66 | // Edge Case: Handle verse with 2 bottles |
| 67 | if (bottles === 2) { |
| 68 | return [ |
| 69 | '2 bottles of beer on the wall, 2 bottles of beer.', |
| 70 | 'Take one down and pass it around, 1 bottle of beer on the wall.' |
| 71 | ]; |
| 72 | } |
| 73 | |
| 74 | // General case for 3 or more bottles |
| 75 | return [ |
| 76 | `${bottles} bottles of beer on the wall, ${bottles} bottles of beer.`, |
| 77 | `Take one down and pass it around, ${bottles - 1} bottles of beer on the wall.` |
| 78 | ]; |
| 79 | }; |
| 80 | |
| 81 | // Handled Edge Cases: |
| 82 | // - Invalid initialBottlesCount (< 0 or > 99) |
| 83 | // - Invalid takeDownCount (< 0) |
| 84 | // - Negative bottle counts during iteration |
| 85 | // - Special wording for 0 bottles (final verse) |
| 86 | // - Special wording for 1 bottle |
| 87 | // - Special wording for 2 bottles |
| 88 | // - Adding empty lines between verses when multiple verses are requested |