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.139 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.138 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.137 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.136 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.135 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.134 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.133 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.132 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.131 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.130 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.129 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.128 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.127 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.126 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.125 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.124 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.123 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.122 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.121 tracks/javascript/exercises/beer-song/example.js
trackler-2.2.1.120 tracks/javascript/exercises/beer-song/example.js