Guest User

Untitled

a guest
Jul 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. // noprotect
  12. // ^^ `noprotect` is here to prevent a bug with jsbin and for loops.
  13.  
  14. function average(numbers) {
  15. var total = numbers[0];
  16. for (var i=1; i < numbers.length; i++) {
  17. total+= numbers[i];
  18. return total/numbers.length;
  19. }
  20.  
  21.  
  22.  
  23.  
  24. /* From here down, you are not expected to
  25. understand.... for now :)
  26.  
  27.  
  28. Nothing to see here!
  29.  
  30. */
  31.  
  32.  
  33. // tests
  34.  
  35. function testFunctionWorks(fn, input, expected) {
  36. if (fn(input) === expected) {
  37. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  38. return true;
  39. }
  40. else {
  41. console.log(
  42. 'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
  43. ' but was ' + fn(input)
  44. );
  45. return false;
  46. }
  47. }
  48.  
  49. (function runTests() {
  50. var numList1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  51. var correctAns1 = 5.5;
  52. var numList2 = [0, -1, 1];
  53. var correctAns2 = 0;
  54.  
  55. var testResults = [
  56. testFunctionWorks(average, numList1, correctAns1),
  57. testFunctionWorks(average, numList2, correctAns2)
  58. ];
  59. var numPassing = testResults.filter(function(result){ return result; }).length;
  60. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.')
  61. })();
  62. </script>
  63.  
  64.  
  65.  
  66. <script id="jsbin-source-javascript" type="text/javascript">// noprotect
  67. // ^^ `noprotect` is here to prevent a bug with jsbin and for loops.
  68.  
  69. function average(numbers) {
  70. var total = numbers[0];
  71. for (var i=1; i < numbers.length; i++) {
  72. total+= numbers[i];
  73. return total/numbers.length;
  74. }
  75.  
  76.  
  77.  
  78.  
  79. /* From here down, you are not expected to
  80. understand.... for now :)
  81.  
  82.  
  83. Nothing to see here!
  84.  
  85. */
  86.  
  87.  
  88. // tests
  89.  
  90. function testFunctionWorks(fn, input, expected) {
  91. if (fn(input) === expected) {
  92. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  93. return true;
  94. }
  95. else {
  96. console.log(
  97. 'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
  98. ' but was ' + fn(input)
  99. );
  100. return false;
  101. }
  102. }
  103.  
  104. (function runTests() {
  105. var numList1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  106. var correctAns1 = 5.5;
  107. var numList2 = [0, -1, 1];
  108. var correctAns2 = 0;
  109.  
  110. var testResults = [
  111. testFunctionWorks(average, numList1, correctAns1),
  112. testFunctionWorks(average, numList2, correctAns2)
  113. ];
  114. var numPassing = testResults.filter(function(result){ return result; }).length;
  115. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.')
  116. })();</script></body>
  117. </html>
Add Comment
Please, Sign In to add comment