Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 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 firstFourItems(array) {
  12. // your code goes here
  13. var arrayFour = array.slice(0,4);
  14. return arrayFour;
  15. }
  16.  
  17.  
  18. function lastThreeItems(array) {
  19. // your code goes here
  20. var arrayThree = array.slice(-3);
  21. return arrayThree
  22. }
  23.  
  24.  
  25.  
  26.  
  27. /* From here down, you are not expected to
  28. understand.... for now :)
  29.  
  30.  
  31. Nothing to see here!
  32.  
  33. */
  34.  
  35.  
  36. // tests
  37.  
  38. function testFunctionWorks(fn, input, expected) {
  39.  
  40. var result = fn(input);
  41. if (
  42. result && result.length === expected.length &&
  43. result.every(function(item) {
  44. return expected.indexOf(item) > -1;
  45. })) {
  46. console.log('SUCCESS: `' + fn.name + '` works!')
  47. return true;
  48. }
  49. else {
  50. console.error('FAILURE: `' + fn.name + '` is not working')
  51. return false;
  52. }
  53. }
  54.  
  55. function runTests() {
  56.  
  57. var list = ["red bull", "monster", "amp", "rockstar", "full throttle"];
  58. var result1 = ["red bull", "monster", "amp", "rockstar"];
  59. var result2 = ["amp", "rockstar", "full throttle"];
  60.  
  61. var testResults = [
  62. testFunctionWorks(firstFourItems, list, result1),
  63. testFunctionWorks(lastThreeItems, list, result2),
  64. ];
  65.  
  66.  
  67. var numPassing = testResults.filter(function(result){ return result; }).length;
  68. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  69. }
  70.  
  71. runTests();
  72. </script>
  73.  
  74.  
  75.  
  76. <script id="jsbin-source-javascript" type="text/javascript">
  77. function firstFourItems(array) {
  78. // your code goes here
  79. var arrayFour = array.slice(0,4);
  80. return arrayFour;
  81. }
  82.  
  83.  
  84. function lastThreeItems(array) {
  85. // your code goes here
  86. var arrayThree = array.slice(-3);
  87. return arrayThree
  88. }
  89.  
  90.  
  91.  
  92.  
  93. /* From here down, you are not expected to
  94. understand.... for now :)
  95.  
  96.  
  97. Nothing to see here!
  98.  
  99. */
  100.  
  101.  
  102. // tests
  103.  
  104. function testFunctionWorks(fn, input, expected) {
  105.  
  106. var result = fn(input);
  107. if (
  108. result && result.length === expected.length &&
  109. result.every(function(item) {
  110. return expected.indexOf(item) > -1;
  111. })) {
  112. console.log('SUCCESS: `' + fn.name + '` works!')
  113. return true;
  114. }
  115. else {
  116. console.error('FAILURE: `' + fn.name + '` is not working')
  117. return false;
  118. }
  119. }
  120.  
  121. function runTests() {
  122.  
  123. var list = ["red bull", "monster", "amp", "rockstar", "full throttle"];
  124. var result1 = ["red bull", "monster", "amp", "rockstar"];
  125. var result2 = ["amp", "rockstar", "full throttle"];
  126.  
  127. var testResults = [
  128. testFunctionWorks(firstFourItems, list, result1),
  129. testFunctionWorks(lastThreeItems, list, result2),
  130. ];
  131.  
  132.  
  133. var numPassing = testResults.filter(function(result){ return result; }).length;
  134. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  135. }
  136.  
  137. runTests();
  138. </script></body>
  139. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement