Last active
August 29, 2015 14:06
-
-
Save austinpray/7b8f337695755508fa14 to your computer and use it in GitHub Desktop.
Kegs and Bottles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var kegsandbottles = (function () { | |
var temp; | |
var output = ''; | |
var counter = 0; | |
var numbers = [5, 7]; | |
var exports = function () { | |
exports.start(); | |
}; | |
var templates = { | |
"tests": [ | |
function (number) { | |
return function (num) { | |
var toString = '' + num; | |
return num > 0 && toString.indexOf(number) > -1; | |
}; | |
}, | |
function (number) { | |
return function (num) { | |
return num > 0 && num % number === 0; | |
}; | |
} | |
], | |
"runner": function () { | |
return function (el) { | |
return el(counter); | |
}; | |
} | |
}; | |
var conditions = { | |
"tests": [], | |
"count": 0, | |
}; | |
var foundStore = {}; | |
var foundStoreKey = function (num) { | |
return conditions.tests.reduce(function (prev, curr) { | |
curr = curr(num) ? '1': '0'; | |
return prev + curr; | |
}, ''); | |
}; | |
var renderMethods = { | |
"console": function () { | |
console.log(output); | |
} | |
}; | |
var render = function () { | |
output = '' + counter + ' '; | |
if(conditions.tests.every(templates.runner())) { | |
output = output + 'bottles and '; | |
} | |
if(conditions.tests.some(templates.runner())) { | |
output = output + 'kegs'; | |
} | |
renderMethods.console(); | |
}; | |
var compute = function () { | |
temp = foundStoreKey(counter); | |
if(typeof foundStore[temp] === 'undefined') { | |
foundStore[temp] = true; | |
conditions.count = conditions.count + 1; | |
render(); | |
} | |
}; | |
// main | |
exports.start = function () { | |
numbers.forEach(function (number) { | |
templates.tests.forEach(function (template) { | |
conditions.tests.push(template(number)); | |
}); | |
}); | |
while (conditions.count < Math.pow(2, conditions.tests.length)) { | |
compute(); | |
counter = counter + 1; | |
} | |
}; | |
return exports; | |
})(); | |
kegsandbottles(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment