| 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 | | // |
| 5 | | |
| 6 | | /** |
| 7 | | * @param {number} initialBottlesCount |
| 8 | | * @param {number} takeDownCount |
| 9 | | * @return {string[]} |
| 1 | /* |
| 2 | * Recites the lyrics to the '99 Bottles of Beer' song. |
| 3 | * |
| 4 | * @param {number} initialBottlesCount - The starting number of bottles (0-99). |
| 5 | * @param {number} takeDownCount - The number of verses to recite (default: 1). |
| 6 | * @return {string[]} - An array of strings representing the lyrics. |
| 10 | 7 | */ |
| 11 | 8 | export const recite = (initialBottlesCount, takeDownCount = 1) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 9 | // Edge Case: Invalid input types |
| 10 | if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') { |
| 11 | throw new Error('Inputs must be numbers'); |
| 12 | } |
| 13 | |
| 14 | // Edge Case: Negative counts |
| 15 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 16 | throw new Error('Counts cannot be negative'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: takeDownCount is 0 |
| 20 | if (takeDownCount === 0) { |
| 21 | return []; |
| 22 | } |
| 23 | |
| 24 | const verses = []; |
| 25 | let currentBottles = initialBottlesCount; |
| 26 | |
| 27 | for (let i = 0; i < takeDownCount; i++) { |
| 28 | // Edge Case: Current bottles count goes below 0 |
| 29 | if (currentBottles < 0) { |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | verses.push(...generateVerse(currentBottles)); |
| 34 | |
| 35 | // Add empty string for spacing between verses, except for the last verse |
| 36 | if (i < takeDownCount - 1) { |
| 37 | verses.push(''); |
| 38 | } |
| 39 | |
| 40 | currentBottles--; |
| 41 | } |
| 42 | |
| 43 | return verses; |
| 13 | 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * Generates a single verse of the beer song based on the number of bottles. |
| 48 | * |
| 49 | * @param {number} bottles - The number of bottles for this verse. |
| 50 | * @return {string[]} - An array containing the two lines of the verse. |
| 51 | */ |
| 52 | const generateVerse = (bottles) => { |
| 53 | // Edge Case: No more bottles |
| 54 | if (bottles === 0) { |
| 55 | return [ |
| 56 | 'No more bottles of beer on the wall, no more bottles of beer.', |
| 57 | 'Go to the store and buy some more, 99 bottles of beer on the wall.' |
| 58 | ]; |
| 59 | } |
| 60 | |
| 61 | // Edge Case: One bottle |
| 62 | if (bottles === 1) { |
| 63 | return [ |
| 64 | '1 bottle of beer on the wall, 1 bottle of beer.', |
| 65 | 'Take it down and pass it around, no more bottles of beer on the wall.' |
| 66 | ]; |
| 67 | } |
| 68 | |
| 69 | // Edge Case: Two bottles |
| 70 | if (bottles === 2) { |
| 71 | return [ |
| 72 | '2 bottles of beer on the wall, 2 bottles of beer.', |
| 73 | 'Take one down and pass it around, 1 bottle of beer on the wall.' |
| 74 | ]; |
| 75 | } |
| 76 | |
| 77 | // General case for 3 or more bottles |
| 78 | return [ |
| 79 | `${bottles} bottles of beer on the wall, ${bottles} bottles of beer.`, |
| 80 | `Take one down and pass it around, ${bottles - 1} bottles of beer on the wall.` |
| 81 | ]; |
| 82 | }; |
| 83 | |
| 84 | // Handled Edge Cases: |
| 85 | // - Invalid input types |
| 86 | // - Negative counts |
| 87 | // - takeDownCount is 0 |
| 88 | // - Current bottles count goes below 0 |
| 89 | // - No more bottles (0) |
| 90 | // - One bottle (1) |
| 91 | // - Two bottles (2) |
| 92 | // - General case (3 or more bottles) |