Skip to content

Instantly share code, notes, and snippets.

@philspitler
Created April 30, 2015 01:57
Show Gist options
  • Select an option

  • Save philspitler/9c4ab42aaa0fdb6841fc to your computer and use it in GitHub Desktop.

Select an option

Save philspitler/9c4ab42aaa0fdb6841fc to your computer and use it in GitHub Desktop.
Array.prototype.shuffle = function (){
var i = this.length, j, temp;
if ( i === 0 ) return;
while ( --i ) {
j = Math.floor( Math.random() * ( i + 1 ) );
temp = this[i];
this[i] = this[j];
this[j] = temp;
}
return this;
};
var houseFactory = (function () {
'use strict';
var thisFormatter;
var data = [
'the horse and the hound and the horn that belonged to',
'the farmer sowing his corn that kept',
'the rooster that crowed in the morn that woke',
'the priest all shaven and shorn that married',
'the man all tattered and torn that kissed',
'the maiden all forlorn that milked',
'the cow with the crumpled horn that tossed',
'the dog that worried',
'the cat that killed',
'the rat that ate',
'the malt that lay in',
'the house that Jack built',
];
return {
init: function (orderer, formatter) {
thisFormatter = formatter;
data = orderer.order(data);
return this;
},
phrase: function (number) {
return this.parts(number).join(' ');
},
parts: function (number) {
var self = this;
return thisFormatter.format(data.slice(Math.max((data.length-1) - number, 0)));
},
recite: function () {
var self = this;
return data.map(function (element, index) {
return self.line(index);
}).join("\n");
},
line: function (number) {
return 'This is ' + this.phrase(number) + ".\n";
}
};
}());
var defaultOrderer = {
order: function (data) {
return data;
}
};
var randomOrderer = {
order: function (data) {
return data.shuffle();
}
};
var defaultFormatter = {
format: function (parts) {
return parts;
}
};
var echoFormatter = {
format: function (parts) {
return parts
.map(function(element) { return [element,element];})
.reduce(function (a,b) { return a.concat(b); });
}
};
//var house = houseFactory.init(defaultOrderer, defaultFormatter);
//console.log(house.recite());
//var randomHouse = houseFactory.init(randomOrderer, defaultFormatter);
//console.log(randomHouse.recite());
//var echoHouse = houseFactory.init(defaultOrderer, echoFormatter);
//console.log(echoHouse.recite());
var randomEchoHouse = houseFactory.init(randomOrderer, echoFormatter);
console.log(randomEchoHouse.recite());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment