Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. /** Generate an array of numbers from min to max.
  2. * @param {number} min - The starting value.
  3. * @param {number} max - The ending value.
  4. * @return {array} An array of all numbers in between min to max.
  5. */
  6. const genArrayOfNumbers = (min, max) => {
  7. return Array(max).fill("").map((n, i) => (n = i + min));
  8. };
  9. /**
  10. * Test if a number is evenly divisible by another number.
  11. * @param {number} num - Most likely the current iteration of a number.
  12. * @param {number} divisor - The number to test against.
  13. * @return {boolean}
  14. */
  15. const divBy = (num, divisor) => {
  16. return num % divisor === 0;
  17. };
  18. /**
  19. * Run the tests on each number and return pass or fail message.
  20. * @param {number} num - Most likely the current iteration of a number.
  21. * @param {array} rules - An array of custom objects, including a test function, a number, and a pass message.
  22. * @return {boolean} An array of pass messages and false values.
  23. */
  24. const runTests = (num, rules) => {
  25. return rules.map(rule => {
  26. return rule.test(num, rule.int) ? rule.msg || " " : false;
  27. });
  28. };
  29. /**
  30. * Return an array of joined pass message or input number.
  31. * @param {array} nums - An array of numbers to test against.
  32. * @param {array} rules - Rules to test the numbers, including a test function, a number, and a pass message.
  33. * @return {array} The array of values to be printed.
  34. */
  35. const mapResults = (nums, rules) => {
  36. return nums.map(n => {
  37. return runTests(n, rules).filter(m => m !== false).join("") || n;
  38. });
  39. };
  40. /**
  41. * Iterate over the array and print the numbers using the specified print method.
  42. * @param {array} arr - The array of values to be printed.
  43. * @param {function} printFn - The print function. Defaults to console.log.
  44. */
  45. const printArr = (arr, printFn = console.log) => {
  46. return function() {
  47. for (let val of arr) {
  48. printFn(val);
  49. }
  50. };
  51. };
  52. /**
  53. * Options object for testing.
  54. */
  55. const options = {
  56. min: 1,
  57. max: 100,
  58. display: console.log,
  59. rules: [
  60. {
  61. test: divBy,
  62. int: 3,
  63. msg: "Crackle"
  64. },
  65. {
  66. test: divBy,
  67. int: 5,
  68. msg: "Pop"
  69. }
  70. ]
  71. };
  72. /**
  73. * Bind and initialize the options object, call genArrayOfNumbers(), mapResults, and return printArr().
  74. */
  75. const initialize = function() {
  76. const arr = genArrayOfNumbers(this.min, this.max);
  77. const result = mapResults(arr, this.rules);
  78. return printArr(result, this.display);
  79. }.bind(options)();
  80. /**
  81. * Call initialize()
  82. */
  83. initialize();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement