Guest User

Untitled

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