daily pastebin goal
13%
SHARE
TWEET

Untitled

a guest Feb 21st, 2018 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function accessFirstItem(array) {
  2.   return(array[0]);
  3. }
  4.  
  5. function accessThirdItem(array) {
  6.   return(array[2]);
  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.   if (fn(input) === expected) {
  21.     console.log(`SUCCESS: "${fn.name}" works on [${input}]`);
  22.     return true;
  23.   } else {
  24.     console.log(
  25.       `FAILURE: ${fn.name}([${input}]) should be ${expected} but was ${fn(
  26.         input
  27.       )}`
  28.     );
  29.     return false;
  30.   }
  31. }
  32.  
  33. function runTests() {
  34.   var list = [1, 4, 9, 16, 25];
  35.   var item1 = 1;
  36.   var item2 = 9;
  37.  
  38.   var testResults = [
  39.     testFunctionWorks(accessFirstItem, list, item1),
  40.     testFunctionWorks(accessThirdItem, list, item2),
  41.   ];
  42.  
  43.   var numPassing = testResults.filter(function(result) {
  44.     return result;
  45.   }).length;
  46.   console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  47. }
  48.  
  49. runTests();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top