Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save philspitler/f31ff450b82cc8d16b17 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 house = (function () {
'use strict';
return {
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',
],
phrase: function (number) {
return this.parts(number).join(' ');
},
parts: function (number) {
return this.data.slice(Math.max((this.data.length-1) - number, 0));
},
recite: function () {
var self = this;
return self.data.map(function (element, index) {
return self.line(index);
}).join("\n");
},
line: function (number) {
return 'This is ' + this.phrase(number) + ".\n";
}
};
}());
var randomHouse = Object.create(house, {
data: {
value: house.data.shuffle()
}
});
var echoHouse = Object.create(house, {
parts: {
value: function (number) {
return this.data.slice(Math.max((this.data.length-1) - number, 0))
.map(function(element) { return [element,element];})
.reduce(function (a,b) { return a.concat(b); });
}
}
});
var randomEchoHouse = Object.create(house, {
data: {
value: randomHouse.data
},
parts: {
value: echoHouse.parts
}
});
console.log(randomEchoHouse.recite());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment