A.2.1

Finished
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 * Generates the lyrics for the '99 Bottles of Beer' song.
3 * Handles special cases like singular/plural bottles, zero bottles, and the final restart verse.
4 *
5 * @param {number} initialBottlesCount - The starting number of bottles (0-99)
6 * @param {number} takeDownCount - How many verses to generate (default: 1)
7 * @return {string[]} - Array of verses, each verse is two lines
108 */
119export const recite = (initialBottlesCount, takeDownCount = 1) => {
12 throw new Error('Remove this line and implement the function');
10 // Edge Case: Invalid input types
11 if (typeof initialBottlesCount !== 'number' || typeof takeDownCount !== 'number') {
12 throw new Error('Both parameters must be numbers');
13 }
14
15 // Edge Case: Negative numbers
16 if (initialBottlesCount < 0 || takeDownCount < 0) {
17 throw new Error('Parameters must be non-negative');
18 }
19
20 // Edge Case: takeDownCount of 0 returns empty array
21 if (takeDownCount === 0) {
22 return [];
23 }
24
25 const verses = [];
26
27 for (let i = 0; i < takeDownCount; i++) {
28 const currentBottles = initialBottlesCount - i;
29
30 // Edge Case: Current bottles count goes below 0
31 if (currentBottles < 0) {
32 break;
33 }
34
35 let line1, line2;
36
37 // Handle special cases for different bottle counts
38 if (currentBottles === 0) {
39 // Edge Case: No more bottles
40 line1 = 'No more bottles of beer on the wall, no more bottles of beer.';
41 line2 = 'Go to the store and buy some more, 99 bottles of beer on the wall.';
42 } else if (currentBottles === 1) {
43 // Edge Case: One bottle
44 line1 = '1 bottle of beer on the wall, 1 bottle of beer.';
45 line2 = 'Take it down and pass it around, no more bottles of beer on the wall.';
46 } else if (currentBottles === 2) {
47 // Edge Case: Two bottles (transition from plural to singular)
48 line1 = '2 bottles of beer on the wall, 2 bottles of beer.';
49 line2 = 'Take one down and pass it around, 1 bottle of beer on the wall.';
50 } else {
51 // Edge Case: Standard plural case (3 or more bottles)
52 line1 = `${currentBottles} bottles of beer on the wall, ${currentBottles} bottles of beer.`;
53 line2 = `Take one down and pass it around, ${currentBottles - 1} bottles of beer on the wall.`;
54 }
55
56 verses.push(line1, line2);
57
58 // Edge Case: After reaching 0 bottles, don't add an extra newline if this is the last verse
59 if (currentBottles === 0 && i < takeDownCount - 1) {
60 verses.push(''); // Add blank line between verses, except after the last verse
61 } else if (currentBottles > 0 && i < takeDownCount - 1) {
62 verses.push(''); // Add blank line between verses
63 }
64 }
65
66 // Handled Edge Cases: Invalid input types, negative numbers, takeDownCount of 0,
67 // current bottles count going below 0, no more bottles, one bottle,
68 // two bottles (transition from plural to singular), standard plural case,
69 // proper spacing between verses
70 return verses;
1371};
Test NameStatus
first generic verse
Pass
last generic verse
Pass
verse with 2 bottles
Pass
verse with 1 bottle
Pass
verse with 0 bottles
Pass
first two verses
Pass
last three verses
Pass
all verses
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.