Advertisement
Guest User

Untitled

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