| 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 99 Bottles of Beer on the Wall. |
| 3 | * |
| 4 | * @param {number} initialBottlesCount - The starting number of bottles (0-99) |
| 5 | * @param {number} takeDownCount - How many verses to recite (default: 1) |
| 6 | * @return {string[]} - Array of verse strings |
| 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('Both parameters must be numbers'); |
| 12 | } |
| 13 | |
| 14 | // Edge Case: Negative numbers |
| 15 | if (initialBottlesCount < 0 || takeDownCount < 0) { |
| 16 | throw new Error('Parameters must be non-negative'); |
| 17 | } |
| 18 | |
| 19 | // Edge Case: takeDownCount is 0 |
| 20 | if (takeDownCount === 0) { |
| 21 | return []; |
| 22 | } |
| 23 | |
| 24 | const result = []; |
| 25 | let currentBottles = initialBottlesCount; |
| 26 | |
| 27 | for (let i = 0; i < takeDownCount; i++) { |
| 28 | // Add the verse lines to the result |
| 29 | const verseLines = createVerse(currentBottles); |
| 30 | result.push(...verseLines); |
| 31 | |
| 32 | // Update currentBottles for next iteration, handling wrap-around from 0 to 99 |
| 33 | if (currentBottles === 0) { |
| 34 | currentBottles = 99; |
| 35 | } else { |
| 36 | currentBottles--; |
| 37 | } |
| 38 | |
| 39 | // Add blank line between verses (except after the last verse) |
| 40 | if (i < takeDownCount - 1) { |
| 41 | result.push(''); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return result; |
| 13 | 46 | }; |
| 47 | |
| 48 | /* |
| 49 | * Creates a single verse of the beer song |
| 50 | * |
| 51 | * @param {number} numBottles - Number of bottles for this verse |
| 52 | * @return {string[]} - Array of verse lines |
| 53 | */ |
| 54 | const createVerse = (numBottles) => { |
| 55 | // Edge Case: No more bottles |
| 56 | if (numBottles === 0) { |
| 57 | return [ |
| 58 | 'No more bottles of beer on the wall, no more bottles of beer.', |
| 59 | 'Go to the store and buy some more, 99 bottles of beer on the wall.' |
| 60 | ]; |
| 61 | } |
| 62 | |
| 63 | // Edge Case: One bottle |
| 64 | if (numBottles === 1) { |
| 65 | return [ |
| 66 | '1 bottle of beer on the wall, 1 bottle of beer.', |
| 67 | 'Take it down and pass it around, no more bottles of beer on the wall.' |
| 68 | ]; |
| 69 | } |
| 70 | |
| 71 | // Edge Case: Two bottles |
| 72 | if (numBottles === 2) { |
| 73 | return [ |
| 74 | '2 bottles of beer on the wall, 2 bottles of beer.', |
| 75 | 'Take one down and pass it around, 1 bottle of beer on the wall.' |
| 76 | ]; |
| 77 | } |
| 78 | |
| 79 | // General case for 3 or more bottles |
| 80 | return [ |
| 81 | `${numBottles} bottles of beer on the wall, ${numBottles} bottles of beer.`, |
| 82 | `Take one down and pass it around, ${numBottles - 1} bottles of beer on the wall.` |
| 83 | ]; |
| 84 | }; |
| 85 | |
| 86 | // Handled Edge Cases: Invalid input types, negative numbers, takeDownCount is 0, going below 0 bottles, no more bottles, one bottle, two bottles |