Guest User

Untitled

a guest
Feb 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. function findLength(array) {
  2. return(array.length);
  3. }
  4.  
  5. function accessLastItem(array) {
  6. return(array[array.length - 1]);
  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. if (fn(input) === expected) {
  22. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  23. return true;
  24. } else {
  25. console.error(
  26. 'FAILURE: `' +
  27. fn.name +
  28. '([' +
  29. input +
  30. '])` should be ' +
  31. expected +
  32. ' but was ' +
  33. fn(input)
  34. );
  35. return false;
  36. }
  37. }
  38.  
  39. function runTests() {
  40. const list = [1, 4, 9, 16, 25];
  41. const originalList = [1, 4, 9, 16, 25];
  42. const length = 5;
  43. const lastItem = 25;
  44.  
  45. const testResults = [
  46. testFunctionWorks(findLength, list, length),
  47. testFunctionWorks(accessLastItem, list, lastItem),
  48. ];
  49.  
  50. const numPassing = testResults.filter(function(result) {
  51. return result;
  52. }).length;
  53. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  54. }
  55.  
  56. runTests();
Add Comment
Please, Sign In to add comment