Guest User

Untitled

a guest
Mar 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. function minusLastItem(array) {
  2. return array.slice(0, array.length -1);
  3. }
  4.  
  5. function copyFirstHalf(array) {
  6. return array.slice(0, array.length / 2);
  7.  
  8. }
  9.  
  10. /* From here down, you are not expected to
  11. understand.... for now :)
  12.  
  13.  
  14. Nothing to see here!
  15.  
  16. */
  17.  
  18. // tests
  19.  
  20. function testFunctionWorks(fn, input, expected) {
  21. const result = fn(input);
  22. if (
  23. result &&
  24. result.length === expected.length &&
  25. result.every(function(item) {
  26. return expected.indexOf(item) > -1;
  27. })
  28. ) {
  29. console.log('SUCCESS: `' + fn.name + '` works!');
  30. return true;
  31. } else {
  32. console.error('FAILURE: `' + fn.name + '` is not working');
  33. return false;
  34. }
  35. }
  36.  
  37. function runTests() {
  38. const list = [
  39. 'red bull',
  40. 'monster',
  41. 'amp',
  42. 'rockstar',
  43. 'full throttle',
  44. 'kickstart',
  45. ];
  46. const result1 = ['red bull', 'monster', 'amp', 'rockstar', 'full throttle'];
  47. const result2 = ['red bull', 'monster', 'amp'];
  48. const list2 = ['lions', 'tigers', 'bears'];
  49. const result3 = ['lions'];
  50.  
  51. const testResults = [
  52. testFunctionWorks(minusLastItem, list, result1),
  53. testFunctionWorks(copyFirstHalf, list, result2),
  54. testFunctionWorks(copyFirstHalf, list2, result3)
  55. ];
  56.  
  57. const numPassing = testResults.filter(function(result) {
  58. return result;
  59. }).length;
  60. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  61. }
Add Comment
Please, Sign In to add comment