Skip to content

Instantly share code, notes, and snippets.

@TotalTechGeek
Last active August 3, 2017 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TotalTechGeek/1dc95922630a5d26b1fa8a2506f57fbe to your computer and use it in GitHub Desktop.
Save TotalTechGeek/1dc95922630a5d26b1fa8a2506f57fbe to your computer and use it in GitHub Desktop.
Exaggerated FizzBuzz Algorithm
// Overexaggerated Configurable FizzBuzz
// Written as a Joke.
// Configuration Format Notes for the Number Parser.
/*
Layout of Configuration: { default : function, rules : array }
default - A function that executes on anything that doesn't complete a rule.
rules - an array of rules that are tested on a number.
Layout of Config Rules -> [Output String|Function, Boolean Function, Terminate Boolean]
Output String|Function - Either a string to print out, or a function that will return a string. The function receives 3 parameters (i,min,max).
Boolean Function - The function that inspects the value and determines whether to output or not. Receives three parameters (i,min,max).
Terminate - Assumed to be true by default. Allows you to print out multiple expressions for a value. Put these near the top. If another rule terminates it before it hits that rule, nothing will be output.
*/
// Runs through a range of values and uses the configuration to generate output.
// min - The lower bound of the integer range.
// max - The upper bound of the integer range.
// config - The configuration for how the numbers should be interpreted. (See above).
function numberRuleRunner(config, min, max)
{
// Set up some constants for rule indexes.
const MESSAGE = 0;
const TEST = 1;
const TERMINATE = 2;
// Sets up default values.
var min = (min || 1) | 0;
var max = (max || 100) | 0;
var config = config || {};
var queue = [];
// Sets up the configuration (if it wasnt set up with all the params).
if (typeof config.out === "undefined") config.out = console.log;
if (typeof config.default === "undefined") config.default = config.out;
if (typeof config.rules === "undefined") config.rules = [];
// Iterates over the values in the range.
for (var i = min; i <= max; i++)
{
// Parses through the rules on the integer.
// If it finds a match that is configured to halt,
// it will stop parsing through the rest of the rules.
config.rules.some(function(rule)
{
// Checks if the rules are valid, and test true.
if (Array.isArray(rule) && rule.length >= 2)
if (typeof rule[TEST] === "function" && rule[TEST](i, min, max))
{
// If it's a string, adds it to the output queue.
if (typeof rule[MESSAGE] === "string")
{
queue.push(rule[MESSAGE]);
}
// Otherwise, if it's a function, execute it and push
// its result to the output queue.
else if (typeof rule[MESSAGE] === "function")
{
queue.push(rule[MESSAGE](i, min, max));
}
// Determines whether it should terminate early or not.
return (typeof rule[TERMINATE] === "undefined") ? true : rule[TERMINATE];
}
});
// Outputs the collected results from matches.
if (queue.length != 0)
{
// Outputs the results.
config.out(queue.join(""));
// Deletes the entire output queue.
queue.splice(0);
}
// if there wasn't a match, execute the default function.
else
{
config.default(i);
}
}
}
// Returns a function that'll check if a value is divisible by x.
function fizzDivisibleBy(x)
{
return (i) => { return i % x === 0; };
}
// Returns a function that'll check if a value is equal to x.
function fizzEqualTo(x)
{
return (i) => { return i === x; };
}
// Runs the standard Fizz Buzz Algorithm
function fizzBuzz(min, max)
{
var min = (min || 1) | 0;
var max = (max || 100) | 0;
var config =
{
rules: [["Fizz", fizzDivisibleBy(3), false],
["Buzz", fizzDivisibleBy(5)]]
};
numberRuleRunner(config, min, max);
}
fizzBuzz();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment