Sha256: ac4743fc976a111a8db77382ec147755973ddc27d40c6cb0676eadedf9d13015

Contents?: true

Size: 1.07 KB

Versions: 211

Compression:

Stored size: 1.07 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, ';
  }

  let 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) {
    let line1 = bottles(number) + ' of beer on the wall, ';
    let line2 = bottles(number).toLowerCase() + ' of beer.\n';
    let line3 = action(number);
    let line4 = nextBottle(number);

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

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

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

export default BeerSong;

Version data entries

211 entries across 211 versions & 1 rubygems

Version Path
trackler-2.1.0.15 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.14 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.13 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.12 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.11 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.10 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.9 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.8 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.7 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.6 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.5 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.4 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.3 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.2 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.1 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.1.0.0 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.0.8.55 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.0.8.54 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.0.8.53 tracks/ecmascript/exercises/beer-song/example.js
trackler-2.0.8.52 tracks/ecmascript/exercises/beer-song/example.js