Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
110
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. /* From here down, you are not expected to
  13. understand.... for now :)
  14.  
  15.  
  16. Nothing to see here!
  17.  
  18. */
  19.  
  20.  
  21. // tests
  22.  
  23. function testFunctionWorks(fn, input, expected) {
  24. if (fn(input) === expected) {
  25. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  26. return true;
  27. }
  28. else {
  29. console.log(
  30. 'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
  31. ' but was ' + fn(input)
  32. );
  33. return false;
  34. }
  35. }
  36.  
  37. (function runTests() {
  38. const numList1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  39. const correctAns1 = 5.5;
  40. const numList2 = [0, -1, 1];
  41. const correctAns2 = 0;
  42.  
  43. const testResults = [
  44. testFunctionWorks(average, numList1, correctAns1),
  45. testFunctionWorks(average, numList2, correctAns2)
  46. ];
  47. const numPassing = testResults.filter(function(result){ return result; }).length;
  48. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.')
  49. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement