Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = class BeerWall {
- static sing(starting = 99, ending = 0) {
- this.validate(starting, ending);
- return [...this.recite(starting, ending)].join('\n\n');
- }
- static *recite(starting, ending) {
- const wall = new BeerWall(starting);
- while ((yield* wall) > ending) {}
- }
- static verse(bottles) {
- return new BeerWall(bottles).verse();
- }
- static validate() {
- if (typeof arguments[0] !== 'number' || typeof arguments[1] !== 'number') {
- throw new TypeError('Invalid type: starting and ending must be numbers');
- }
- if (arguments[0] > 99 || arguments[0] < 0) {
- throw new RangeError('Invalid starting');
- }
- if (arguments[1] > 99 || arguments[1] < 0) {
- throw new RangeError('Invalid ending');
- }
- if (arguments[0] < arguments[1]) {
- throw new RangeError('starting must be greater or equal to ending');
- }
- }
- constructor(bottles) {
- this.bottles = bottles;
- }
- *[Symbol.iterator]() {
- const current = this.bottles;
- yield this.verse();
- return current;
- }
- verse() {
- return (
- `${this.Quantity} ${this.container} of beer on the wall, ` +
- `${this.quantity} ${this.container} of beer.\n` +
- `${this.action()}, ` +
- `${this.quantity} ${this.container} of beer on the wall.`
- );
- }
- get Quantity() {
- const quantity = this.quantity;
- return quantity.charAt(0).toUpperCase() + quantity.substr(1);
- }
- get quantity() {
- switch (this.bottles) {
- case 0:
- return 'no more';
- default:
- return this.bottles.toString();
- }
- }
- get container() {
- switch (this.bottles) {
- case 1:
- return 'bottle';
- default:
- return 'bottles';
- }
- }
- get pronoun() {
- switch (this.bottles) {
- case 1:
- return 'it';
- default:
- return 'one';
- }
- }
- action() {
- const pronoun = this.pronoun;
- const bottles = this.bottles--;
- if (bottles === 0) {
- this.bottles = 99;
- }
- switch (bottles) {
- case 0:
- return 'Go to the store and buy some more';
- default:
- return `Take ${pronoun} down and pass it around`;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement