Guest User

Untitled

a guest
Oct 21st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. function average(numbers) {
  2. let total = numbers[0];
  3. for (let i=1; i < numbers.length; i++) {
  4. total += numbers[i];
  5. }
  6. return total/numbers.length;
  7. }
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14. /* From here down, you are not expected to
  15. understand.... for now :)
  16.  
  17.  
  18. Nothing to see here!
  19.  
  20. */
  21.  
  22.  
  23. // tests
  24.  
  25. function testFunctionWorks(fn, input, expected) {
  26. if (fn(input) === expected) {
  27. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  28. return true;
  29. }
  30. else {
  31. console.log(
  32. 'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
  33. ' but was ' + fn(input)
  34. );
  35. return false;
  36. }
  37. }
  38.  
  39. (function runTests() {
  40. const numList1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  41. const correctAns1 = 5.5;
  42. const numList2 = [0, -1, 1];
  43. const correctAns2 = 0;
  44.  
  45. const testResults = [
  46. testFunctionWorks(average, numList1, correctAns1),
  47. testFunctionWorks(average, numList2, correctAns2)
  48. ];
  49. const numPassing = testResults.filter(function(result){ return result; }).length;
  50. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.')
  51. })();
Add Comment
Please, Sign In to add comment