Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Generate an array of numbers from min to max.
- * @param {number} min - The starting value.
- * @param {number} max - The ending value.
- * @return {array} An array of all numbers in between min to max.
- */
- const genArrayOfNumbers = (min, max) => {
- return Array(max).fill("").map((n, i) => (n = i + min));
- };
- /**
- * Test if a number is evenly divisible by another number.
- * @param {number} num - Most likely the current iteration of a number.
- * @param {number} divisor - The number to test against.
- * @return {boolean}
- */
- const divBy = (num, divisor) => {
- return num % divisor === 0;
- };
- /**
- * Run the tests on each number and return pass or fail message.
- * @param {number} num - Most likely the current iteration of a number.
- * @param {array} rules - An array of custom objects, including a test function, a number, and a pass message.
- * @return {boolean} An array of pass messages and false values.
- */
- const runTests = (num, rules) => {
- return rules.map(rule => {
- return rule.test(num, rule.int) ? rule.msg || " " : false;
- });
- };
- /**
- * Return an array of joined pass message or input number.
- * @param {array} nums - An array of numbers to test against.
- * @param {array} rules - Rules to test the numbers, including a test function, a number, and a pass message.
- * @return {array} The array of values to be printed.
- */
- const mapResults = (nums, rules) => {
- return nums.map(n => {
- return runTests(n, rules).filter(m => m !== false).join("") || n;
- });
- };
- /**
- * Iterate over the array and print the numbers using the specified print method.
- * @param {array} arr - The array of values to be printed.
- * @param {function} printFn - The print function. Defaults to console.log.
- */
- const printArr = (arr, printFn = console.log) => {
- return function() {
- for (let val of arr) {
- printFn(val);
- }
- };
- };
- /**
- * Options object for testing.
- */
- const options = {
- min: 1,
- max: 100,
- display: console.log,
- rules: [
- {
- test: divBy,
- int: 3,
- msg: "Crackle"
- },
- {
- test: divBy,
- int: 5,
- msg: "Pop"
- }
- ]
- };
- /**
- * Bind and initialize the options object, call genArrayOfNumbers(), mapResults, and return printArr().
- */
- const initialize = function() {
- const arr = genArrayOfNumbers(this.min, this.max);
- const result = mapResults(arr, this.rules);
- return printArr(result, this.display);
- }.bind(options)();
- /**
- * Call initialize()
- */
- initialize();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement