Guest User

Untitled

a guest
Mar 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. function accessFirstItem(array) {
  2. return array[0];
  3. }
  4.  
  5. function accessThirdItem(array) {
  6. return array[2];
  7. }
  8.  
  9. // tests
  10.  
  11. function testFunctionWorks(fn, input, expected) {
  12. if (fn(input) === expected) {
  13. console.log(`SUCCESS: "${fn.name}" works on [${input}]`);
  14. return true;
  15. } else {
  16. console.log(
  17. `FAILURE: ${fn.name}([${input}]) should be ${expected} but was ${fn(
  18. input
  19. )}`
  20. );
  21. return false;
  22. }
  23. }
  24.  
  25. function runTests() {
  26. var list = [1, 4, 9, 16, 25];
  27. var item1 = 1;
  28. var item2 = 9;
  29.  
  30. var testResults = [
  31. testFunctionWorks(accessFirstItem, list, item1),
  32. testFunctionWorks(accessThirdItem, list, item2),
  33. ];
  34.  
  35. var numPassing = testResults.filter(function(result) {
  36. return result;
  37. }).length;
  38. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  39. }
  40.  
  41. runTests();
Add Comment
Please, Sign In to add comment