Guest User

Untitled

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