Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. function firstFourItems(array) {
  2. let newArr = array.slice(0, 4);
  3. return newArr;
  4. }
  5.  
  6. function lastThreeItems(array) {
  7. let newArr = array.slice(-3);
  8. return newArr;
  9. }
  10.  
  11. /* From here down, you are not expected to
  12. understand.... for now :)
  13.  
  14.  
  15. Nothing to see here!
  16.  
  17. */
  18.  
  19. // tests
  20.  
  21. function testFunctionWorks(fn, input, expected) {
  22. const result = fn(input);
  23. if (
  24. result &&
  25. result.length === expected.length &&
  26. result.every(function(item) {
  27. return expected.indexOf(item) > -1;
  28. })
  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. const list = ['red bull', 'monster', 'amp', 'rockstar', 'full throttle'];
  40. const result1 = ['red bull', 'monster', 'amp', 'rockstar'];
  41. const result2 = ['amp', 'rockstar', 'full throttle'];
  42.  
  43. const testResults = [
  44. testFunctionWorks(firstFourItems, list, result1),
  45. testFunctionWorks(lastThreeItems, list, result2),
  46. ];
  47.  
  48. const numPassing = testResults.filter(function(result) {
  49. return result;
  50. }).length;
  51. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  52. }
  53.  
  54. runTests();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement