SHARE
TWEET

Untitled

a guest Apr 26th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function shortWords(array) {
  2.   return array.filter(function(num) {
  3.     return num.length < 5;
  4.   });
  5. }
  6.  
  7. /* From here down, you are not expected to
  8.    understand.... for now :)  
  9.    
  10.    
  11.    Nothing to see here!
  12.    
  13. */
  14.  
  15. // tests
  16.  
  17. function testFunctionWorks(fn, input, expected) {
  18.   const result = fn(input);
  19.   if (
  20.     result &&
  21.     result.length === expected.length &&
  22.     result.every(function(item) {
  23.       return expected.indexOf(item) > -1;
  24.     })
  25.   ) {
  26.     console.log('SUCCESS: `' + fn.name + '` works!');
  27.     return true;
  28.   } else {
  29.     console.error(
  30.       'FAILURE: `' +
  31.         fn.name +
  32.         '([' +
  33.         input +
  34.         '])` should be ' +
  35.         expected +
  36.         ' but was ' +
  37.         fn(input)
  38.     );
  39.     return false;
  40.   }
  41. }
  42.  
  43. function runTests() {
  44.   const input1 = ['cat', 'oblivion', 'dog', 'patriarchy', 'blue', 'house'];
  45.   const result1 = ['cat', 'dog', 'blue'];
  46.   const input2 = ['rainbow', 'the', 'big', 'broom'];
  47.   const result2 = ['the', 'big'];
  48.  
  49.   const testResults = [
  50.     testFunctionWorks(shortWords, input1, result1),
  51.     testFunctionWorks(shortWords, input2, result2),
  52.   ];
  53.  
  54.   const numPassing = testResults.filter(function(result) {
  55.     return result;
  56.   }).length;
  57.   console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  58. }
  59.  
  60. 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