Guest User

Untitled

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