Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 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. return array.slice(0,(array.length-1));
  13. // your code goes here
  14. }
  15.  
  16.  
  17. function copyFirstHalf(array) {
  18. return array.slice(0, (array.length/2));
  19. // your code goes here
  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. var result = fn(input);
  37. if (
  38. result && result.length === expected.length &&
  39. result.every(function(item) {
  40. return expected.indexOf(item) > -1;
  41. })) {
  42. console.log('SUCCESS: `' + fn.name + '` works!');
  43. return true;
  44. } else {
  45. console.error('FAILURE: `' + fn.name + '` is not working');
  46. return false;
  47. }
  48. }
  49.  
  50. function runTests() {
  51.  
  52. var list = ["red bull", "monster", "amp", "rockstar", "full throttle", "kickstart"];
  53. var result1 = ["red bull", "monster", "amp", "rockstar", "full throttle"];
  54. var result2 = ["red bull", "monster", "amp"];
  55.  
  56. var testResults = [
  57. testFunctionWorks(minusLastItem, list, result1),
  58. testFunctionWorks(copyFirstHalf, list, result2),
  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">
  72. function minusLastItem(array) {
  73. return array.slice(0,(array.length-1));
  74. // your code goes here
  75. }
  76.  
  77.  
  78. function copyFirstHalf(array) {
  79. return array.slice(0, (array.length/2));
  80. // your code goes here
  81. }
  82.  
  83.  
  84.  
  85. /* From here down, you are not expected to
  86. understand.... for now :)
  87.  
  88.  
  89. Nothing to see here!
  90.  
  91. */
  92.  
  93.  
  94. // tests
  95.  
  96. function testFunctionWorks(fn, input, expected) {
  97. var result = fn(input);
  98. if (
  99. result && result.length === expected.length &&
  100. result.every(function(item) {
  101. return expected.indexOf(item) > -1;
  102. })) {
  103. console.log('SUCCESS: `' + fn.name + '` works!');
  104. return true;
  105. } else {
  106. console.error('FAILURE: `' + fn.name + '` is not working');
  107. return false;
  108. }
  109. }
  110.  
  111. function runTests() {
  112.  
  113. var list = ["red bull", "monster", "amp", "rockstar", "full throttle", "kickstart"];
  114. var result1 = ["red bull", "monster", "amp", "rockstar", "full throttle"];
  115. var result2 = ["red bull", "monster", "amp"];
  116.  
  117. var testResults = [
  118. testFunctionWorks(minusLastItem, list, result1),
  119. testFunctionWorks(copyFirstHalf, list, result2),
  120. ];
  121.  
  122.  
  123. var numPassing = testResults.filter(function(result){ return result; }).length;
  124. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  125. }
  126.  
  127. runTests();
  128. </script></body>
  129. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement