Sha256: 8d739b501249a557838f6c54c97e37e689f2dcc8e1851085046f295c477a41d2

Contents?: true

Size: 1.08 KB

Versions: 185

Compression:

Stored size: 1.08 KB

Contents

function bottles(number) {
  if (number === 0) {
    return 'No more bottles';
  }

  if (number === 1) {
    return '1 bottle';
  }

  return `${number} bottles`;
}

function action(currentVerse) {
  if (currentVerse === 0) {
    return 'Go to the store and buy some more, ';
  }

  const sbj = currentVerse === 1 ? 'it' : 'one';
  return `Take ${sbj} down and pass it around, `;
}

function nextBottle(currentVerse) {
  return `${bottles(nextVerse(currentVerse)).toLowerCase()} of beer on the wall.\n`;
}

function nextVerse(currentVerse) {
  return currentVerse === 0 ? 99 : currentVerse - 1;
}

class BeerSong {
  static verse(number) {
    const line1 = `${bottles(number)} of beer on the wall, `;
    const line2 = `${bottles(number).toLowerCase()} of beer.\n`;
    const line3 = action(number);
    const line4 = nextBottle(number);

    return [line1, line2, line3, line4].join('');
  }

  static sing(first = 99, last = 0) {
    const verses = [];
    for (let i = first; i >= last; i--) {
      verses.push(this.verse(i));
    }

    return verses.join('\n');
  }
}

export default BeerSong;

Version data entries

185 entries across 185 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.179 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.178 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.177 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.176 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.175 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.174 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.173 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.172 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.171 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.170 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.169 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.167 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.166 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.165 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.164 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.163 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.162 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.161 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.2.1.160 tracks/ecmascript/exercises/beer-song/example.js