beer-song.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Beer {
  static verse(n) {
    if (n == 0) {
      return "No more bottles of beer on the wall, no more bottles of beer.\n"
        + "Go to the store and buy some more, 99 bottles of beer on the wall.\n";

    } else if(n == 1) {
      return "1 bottle of beer on the wall, 1 bottle of beer.\n"
        + "Take it down and pass it around, no more bottles of beer on the wall.\n";

    } else if(n == 2) {
      return "2 bottles of beer on the wall, 2 bottles of beer.\n"
        + "Take one down and pass it around, 1 bottle of beer on the wall.\n";

    } else {
      return `${n} bottles of beer on the wall, ${n} bottles of beer.\n`
        + `Take one down and pass it around, ${n - 1} bottles of beer on the wall.\n`;
    }
  }

  static sing(from = 99, to = 0) {
    let v = "";
    for (let i = from; i >= to; i--) {
      v += Beer.verse(i) + "\n";
    }
    return v.trim() + "\n";
  }
}

export default Beer;

@codingthat and @asgs think this looks great

Comments

why can't n == 2 be handled by the else block?

EDIT - just realized the bottle vs bottles difference. please ignore.

asgs commented 14 July 2017 at 19:41 UTC

You're not logged in right now. Please login via GitHub to comment