Guest User

Untitled

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