Guest User

Untitled

a guest
Mar 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. function average(numbers) {
  2. let total= 0;
  3. for (let i= 0; i < numbers.length; i++){
  4. total= total+numbers[i];}
  5. let av= total / numbers.length;
  6.  
  7. return av;
  8. }
  9.  
  10. /* From here down, you are not expected to
  11. understand.... for now :)
  12.  
  13.  
  14. Nothing to see here!
  15.  
  16. */
  17.  
  18. // tests
  19.  
  20. function testFunctionWorks(fn, input, expected) {
  21. if (fn(input) === expected) {
  22. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  23. return true;
  24. } else {
  25. console.log(
  26. 'FAILURE: `' +
  27. fn.name +
  28. '([' +
  29. input +
  30. '])` should be ' +
  31. expected +
  32. ' but was ' +
  33. 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) {
  50. return result;
Add Comment
Please, Sign In to add comment