SHARE
TWEET

Untitled

a guest Apr 26th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function max(numbers) {
  2.    let currentMax = numbers[0];
  3.   for (let i = 0; i < numbers.length; i++) {
  4.     if (numbers[i] > currentMax) {
  5.       currentMax = numbers[i];
  6.     }
  7.   }
  8.   return currentMax;
  9. }
  10.  
  11. function min(numbers) {
  12.  let currentMin = numbers[0];
  13.   for (let i = 0; i < numbers.length; i++) {
  14.     if (numbers [i] < currentMin) {
  15.       currentMin = numbers [i];
  16.     }
  17.   }
  18.   return currentMin;
  19. }
  20.  
  21. /* From here down, you are not expected to
  22.    understand.... for now :)  
  23.    
  24.    
  25.    Nothing to see here!
  26.    
  27. */
  28.  
  29. // tests
  30.  
  31. function testFunctionWorks(fn, input, expected) {
  32.   if (fn(input) === expected) {
  33.     console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  34.     return true;
  35.   } else {
  36.     console.log(
  37.       'FAILURE: `' +
  38.         fn.name +
  39.         '([' +
  40.         input +
  41.         '])` should be ' +
  42.         expected +
  43.         ' but was ' +
  44.         fn(input)
  45.     );
  46.     return false;
  47.   }
  48. }
  49.  
  50. function testEmpty(fn) {
  51.   if (fn([]) === null || fn([]) == undefined) {
  52.     console.log(`SUCCESS: ${fn.name} works on empty arrays`);
  53.     return true;
  54.   } else {
  55.     console.log(
  56.       `FAILURE: ${fn.name} should return undefined or null for empty arrays`
  57.     );
  58.     return false;
  59.   }
  60. }
  61.  
  62. (function runTests() {
  63.   // we'll use the variables in our test cases
  64.   const numList1 = [-5, 28, 98, -20013, 0.7878, 22, 115];
  65.   const realMin1 = numList1[3];
  66.   const realMax1 = numList1[6];
  67.   const numList2 = [0, 1, 2, 3, 4];
  68.   const realMin2 = numList2[0];
  69.   const realMax2 = numList2[4];
  70.  
  71.   const testResults = [
  72.     testFunctionWorks(max, numList1, realMax1),
  73.     testFunctionWorks(max, numList2, realMax2),
  74.     testFunctionWorks(min, numList1, realMin1),
  75.     testFunctionWorks(min, numList2, realMin2),
  76.     testEmpty(max),
  77.     testEmpty(min),
  78.   ];
  79.  
  80.   const numPassing = testResults.filter(function(result) {
  81.     return result;
  82.   }).length;
  83.   console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  84. })();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top