Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 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. // tests
  13.  
  14. function testFunctionWorks(fn, input, expected) {
  15. if (fn(input) === expected) {
  16. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  17. return true;
  18. } else {
  19. console.log(
  20. 'FAILURE: `' +
  21. fn.name +
  22. '([' +
  23. input +
  24. '])` should be ' +
  25. expected +
  26. ' but was ' +
  27. fn(input)
  28. );
  29. return false;
  30. }
  31. }
  32.  
  33. (function runTests() {
  34. const numList1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  35. const correctAns1 = 5.5;
  36. const numList2 = [0, -1, 1];
  37. const correctAns2 = 0;
  38.  
  39. const testResults = [
  40. testFunctionWorks(average, numList1, correctAns1),
  41. testFunctionWorks(average, numList2, correctAns2),
  42. ];
  43. const numPassing = testResults.filter(function(result) {
  44. return result;
  45. }).length;
  46. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  47. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement