Sha256: 8f2641c6efe322db409591db00cfe4a96348ba8650e341aa4c42c06fc2f8bfde

Contents?: true

Size: 1.6 KB

Versions: 124

Compression:

Stored size: 1.6 KB

Contents

(function () {
  'use strict';

  function bottles(number) {
    var str = '';

    if (number === 0) {
      str = 'No more bottles';
    } else if (number === 1) {
      str = '1 bottle';
    } else {
      str = number + ' bottles';
    }

    return str;
  }

  function action(currentVerse) {
    var sbj;
    var str = '';

    if (currentVerse === 0) {
      str = 'Go to the store and buy some more, ';
    } else {
      sbj = (currentVerse === 1 ? 'it' : 'one');
      str = 'Take ' + sbj + ' down and pass it around, ';
    }

    return str;
  }

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

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

  function BeerSong() {}

  BeerSong.prototype.sing = function (first, last) {
    var firstVerseBottleCount = first;
    var lastVerseBottleCount = last;

    if (typeof (firstVerseBottleCount) === 'undefined') {
      firstVerseBottleCount = 99;
    }
    if (typeof (lastVerseBottleCount) === 'undefined') {
      lastVerseBottleCount = 0;
    }

    var verses = [];
    for (var i = firstVerseBottleCount; i >= lastVerseBottleCount; i--) {
      verses.push(this.verse(i));
    }

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

  BeerSong.prototype.verse = function (number) {
    var line1 = bottles(number) + ' of beer on the wall, ';
    var line2 = bottles(number).toLowerCase() + ' of beer.\n';
    var line3 = action(number);
    var line4 = nextBottle(number);

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

  module.exports = BeerSong;
})();

Version data entries

124 entries across 124 versions & 1 rubygems

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