Advertisement
Guest User

Untitled

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