Guest User

Untitled

a guest
Dec 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. //=========================
  2. // MAX / MIN DRILL |
  3. //=========================
  4.  
  5. function max(numbers) {
  6. let maxNumber = numbers[0];
  7.  
  8. for (let num = 0; num < numbers.length; num++) {
  9. if(numbers[num] > maxNumber) {
  10. maxNumber = numbers[num];
  11. }
  12. }
  13.  
  14. return maxNumber;
  15. }
  16.  
  17. function min(numbers) {
  18. let minNumber = numbers[0];
  19.  
  20. for (let num = 0; num < numbers.length; num++) {
  21. if(numbers[num] < minNumber) {
  22. minNumber = numbers[num];
  23. }
  24. }
  25.  
  26. return minNumber;
  27. }
  28.  
  29. /* From here down, you are not expected to
  30. understand.... for now :)
  31.  
  32.  
  33. Nothing to see here!
  34.  
  35. */
  36.  
  37. // tests
  38.  
  39. function testFunctionWorks(fn, input, expected) {
  40. if (fn(input) === expected) {
  41. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  42. return true;
  43. } else {
  44. console.log(
  45. 'FAILURE: `' +
  46. fn.name +
  47. '([' +
  48. input +
  49. '])` should be ' +
  50. expected +
  51. ' but was ' +
  52. fn(input)
  53. );
  54. return false;
  55. }
  56. }
  57.  
  58. function testEmpty(fn) {
  59. if (fn([]) === null || fn([]) == undefined) {
  60. console.log(`SUCCESS: ${fn.name} works on empty arrays`);
  61. return true;
  62. } else {
  63. console.log(
  64. `FAILURE: ${fn.name} should return undefined or null for empty arrays`
  65. );
  66. return false;
  67. }
  68. }
  69.  
  70. (function runTests() {
  71. // we'll use the variables in our test cases
  72. const numList1 = [-5, 28, 98, -20013, 0.7878, 22, 115];
  73. const realMin1 = numList1[3];
  74. const realMax1 = numList1[6];
  75. const numList2 = [0, 1, 2, 3, 4];
  76. const realMin2 = numList2[0];
  77. const realMax2 = numList2[4];
  78.  
  79. const testResults = [
  80. testFunctionWorks(max, numList1, realMax1),
  81. testFunctionWorks(max, numList2, realMax2),
  82. testFunctionWorks(min, numList1, realMin1),
  83. testFunctionWorks(min, numList2, realMin2),
  84. testEmpty(max),
  85. testEmpty(min),
  86. ];
  87.  
  88. const numPassing = testResults.filter(function(result) {
  89. return result;
  90. }).length;
  91. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  92. })();
  93.  
  94.  
  95.  
  96.  
  97.  
  98. //==================================================================
  99.  
  100. //=========================
  101. // COMPUTE THE AVERAGE |
  102. //=========================
  103.  
  104. function average(numbers) {
  105. let numTotal = numbers[0];
  106.  
  107. for(let num = 1; num < numbers.length; num++){
  108. numTotal += numbers[num];
  109. }
  110.  
  111. return numTotal / numbers.length;
  112. }
  113.  
  114. /* From here down, you are not expected to
  115. understand.... for now :)
  116.  
  117.  
  118. Nothing to see here!
  119.  
  120. */
  121.  
  122. // tests
  123.  
  124. function testFunctionWorks(fn, input, expected) {
  125. if (fn(input) === expected) {
  126. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  127. return true;
  128. } else {
  129. console.log(
  130. 'FAILURE: `' +
  131. fn.name +
  132. '([' +
  133. input +
  134. '])` should be ' +
  135. expected +
  136. ' but was ' +
  137. fn(input)
  138. );
  139. return false;
  140. }
  141. }
  142.  
  143. (function runTests() {
  144. const numList1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  145. const correctAns1 = 5.5;
  146. const numList2 = [0, -1, 1];
  147. const correctAns2 = 0;
  148.  
  149. const testResults = [
  150. testFunctionWorks(average, numList1, correctAns1),
  151. testFunctionWorks(average, numList2, correctAns2),
  152. ];
  153. const numPassing = testResults.filter(function(result) {
  154. return result;
  155. }).length;
  156. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  157. })();
  158.  
  159.  
  160.  
  161.  
  162.  
  163. //==================================================================
  164.  
  165. //=========================
  166. // FIZZ BUZZ |
  167. //=========================
  168.  
  169. function fizzBuzz(countTo) {
  170. const array = [];
  171.  
  172. for (let num = 1; num <= countTo; num++) {
  173. if (num % 15 === 0) {
  174. array.push('fizzbuzz');
  175. }
  176. else if (num % 5 === 0) {
  177. array.push('buzz');
  178. }
  179. else if (num % 3 === 0) {
  180. array.push('fizz');
  181. }
  182. else {
  183. array.push(num);
  184. }
  185. }
  186.  
  187. return array;
  188. }
  189.  
  190.  
  191. /* From here down, you are not expected to
  192. understand.... for now :)
  193.  
  194.  
  195. Nothing to see here!
  196.  
  197. */
  198.  
  199. // tests
  200. (function testFizzBuzz() {
  201. // we'll use the variables in our test cases
  202. const countTo = 16;
  203. const expected = [
  204. 1,
  205. 2,
  206. 'fizz',
  207. 4,
  208. 'buzz',
  209. 'fizz',
  210. 7,
  211. 8,
  212. 'fizz',
  213. 'buzz',
  214. 11,
  215. 'fizz',
  216. 13,
  217. 14,
  218. 'fizzbuzz',
  219. 16,
  220. ];
  221.  
  222. const actual = fizzBuzz(countTo) || [];
  223.  
  224. if (
  225. expected.length === actual.length &&
  226. expected.every(function(item, index) {
  227. return actual[index] === item;
  228. })
  229. ) {
  230. console.log('SUCCESS: fizzBuzz is working');
  231. } else {
  232. console.log('FAILURE: fizzBuzz is not working');
  233. }
  234. })();
Add Comment
Please, Sign In to add comment