Guest User

Untitled

a guest
Oct 24th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. function shortWords(array) {
  2. // your code goes here
  3. return array.filter(function(string) {
  4. return string.length <5;
  5. });
  6. }
  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. const result = fn(input);
  26. if (
  27. result && result.length === expected.length &&
  28. result.every(function(item) {
  29. return expected.indexOf(item) > -1;
  30. })) {
  31. console.log('SUCCESS: `' + fn.name + '` works!')
  32. return true;
  33. }
  34. else {
  35. console.error('FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
  36. ' but was ' + fn(input))
  37. return false;
  38. }
  39. }
  40.  
  41. function runTests() {
  42.  
  43. const input1 = ['cat', 'oblivion', 'dog', 'patriarchy', 'blue', 'house'];
  44. const result1 = ['cat', 'dog', 'blue'];
  45. const input2 = ['rainbow', 'the', 'big', 'broom'];
  46. const result2 = ['the', 'big'];
  47.  
  48. const testResults = [
  49. testFunctionWorks(shortWords, input1, result1),
  50. testFunctionWorks(shortWords, input2, result2),
  51. ];
  52.  
  53.  
  54.  
  55. const numPassing = testResults.filter(function(result){ return result; }).length;
  56. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  57. }
  58.  
  59. runTests();
Add Comment
Please, Sign In to add comment