Advertisement
Guest User

Untitled

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