Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 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.  
  10.  
  11.  
  12. /* From here down, you are not expected to
  13. understand.... for now :)
  14.  
  15.  
  16. Nothing to see here!
  17.  
  18. */
  19.  
  20.  
  21. // tests
  22.  
  23. function testFunctionWorks(fn, input, expected) {
  24.  
  25. if (fn(input) === expected) {
  26. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  27. return true;
  28. } else {
  29. console.log(
  30. 'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
  31. ' but was ' + fn(input)
  32. );
  33. return false;
  34. }
  35. }
  36.  
  37.  
  38. function runTests() {
  39.  
  40. var list = [1, 4, 9, 16, 25]
  41. var item1 = 1
  42. var item2 = 9
  43.  
  44. var testResults = [
  45. testFunctionWorks(accessFirstItem, list, item1),
  46. testFunctionWorks(accessThirdItem, list, item2),
  47. ];
  48.  
  49.  
  50.  
  51. var numPassing = testResults.filter(function (result) {
  52. return result;
  53. }).length;
  54. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  55. }
  56.  
  57. runTests();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement