Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 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 minusLastItem(array) {
  12. array.pop();
  13. return array;
  14. }
  15.  
  16.  
  17. function copyFirstHalf(array) {
  18. return array.slice(0, 3);
  19. }
  20.  
  21. // tests
  22.  
  23. function testFunctionWorks(fn, input, expected) {
  24. var result = fn(input);
  25. if (
  26. result && result.length === expected.length &&
  27. result.every(function(item) {
  28. return expected.indexOf(item) > -1;
  29. })) {
  30. console.log('SUCCESS: `' + fn.name + '` works!');
  31. return true;
  32. } else {
  33. console.error('FAILURE: `' + fn.name + '` is not working');
  34. return false;
  35. }
  36. }
  37.  
  38. function runTests() {
  39.  
  40. var list = ["red bull", "monster", "amp", "rockstar", "full throttle", "kickstart"];
  41. var result1 = ["red bull", "monster", "amp", "rockstar", "full throttle"];
  42. var result2 = ["red bull", "monster", "amp"];
  43.  
  44. var testResults = [
  45. testFunctionWorks(minusLastItem, list, result1),
  46. testFunctionWorks(copyFirstHalf, list, result2),
  47. ];
  48.  
  49.  
  50. var numPassing = testResults.filter(function(result){ return result; }).length;
  51. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  52. }
  53.  
  54. runTests();
  55. </script>
  56.  
  57.  
  58.  
  59. <script id="jsbin-source-javascript" type="text/javascript">
  60. function minusLastItem(array) {
  61. array.pop();
  62. return array;
  63. }
  64.  
  65.  
  66. function copyFirstHalf(array) {
  67. return array.slice(0, 3);
  68. }
  69.  
  70. // tests
  71.  
  72. function testFunctionWorks(fn, input, expected) {
  73. var result = fn(input);
  74. if (
  75. result && result.length === expected.length &&
  76. result.every(function(item) {
  77. return expected.indexOf(item) > -1;
  78. })) {
  79. console.log('SUCCESS: `' + fn.name + '` works!');
  80. return true;
  81. } else {
  82. console.error('FAILURE: `' + fn.name + '` is not working');
  83. return false;
  84. }
  85. }
  86.  
  87. function runTests() {
  88.  
  89. var list = ["red bull", "monster", "amp", "rockstar", "full throttle", "kickstart"];
  90. var result1 = ["red bull", "monster", "amp", "rockstar", "full throttle"];
  91. var result2 = ["red bull", "monster", "amp"];
  92.  
  93. var testResults = [
  94. testFunctionWorks(minusLastItem, list, result1),
  95. testFunctionWorks(copyFirstHalf, list, result2),
  96. ];
  97.  
  98.  
  99. var numPassing = testResults.filter(function(result){ return result; }).length;
  100. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  101. }
  102.  
  103. runTests();
  104. </script></body>
  105. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement