Guest User

Untitled

a guest
Mar 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. function divisibleBy5(array) {
  2. function isDivisible(num) {
  3. return num % 5 === 0;
  4. }
  5. return array.find(isDivisible);
  6. }
  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.  
  18. // tests
  19.  
  20. function testFunctionWorks(fn, input, expected) {
  21.  
  22. if (fn(input) === expected) {
  23. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  24. return true;
  25. }
  26. else {
  27. console.error(
  28. 'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
  29. ' but was ' + fn(input)
  30. );
  31. return false;
  32. }
  33. }
  34.  
  35. function runTests() {
  36.  
  37. var input1 = [13, 22, 4, 47, 15, 35, 82];
  38. var result1 = 15;
  39. var input2 = [25, 20, 15, 10, 5];
  40. var result2 = 25;
  41.  
  42. var testResults = [
  43. testFunctionWorks(divisibleBy5, input1, result1),
  44. testFunctionWorks(divisibleBy5, input2, result2),
  45. ];
  46.  
  47.  
  48.  
  49. var numPassing = testResults.filter(function(result){ return result; }).length;
  50. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  51. }
  52.  
  53. runTests();
Add Comment
Please, Sign In to add comment