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.78 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.77 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.76 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.75 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.74 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.73 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.72 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.71 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.70 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.69 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.68 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.67 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.66 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.65 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.64 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.63 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.62 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.61 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.60 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.59 tracks/javascript/exercises/beer-song/example.js