Guest User

Untitled

a guest
Apr 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. function divisibleBy5(array) {
  2. // your code here
  3. return array.find(num => num % 5 === 0);
  4. }
  5.  
  6. /*
  7. array.find(function(num) {
  8. return num % 5 === 0;
  9. }
  10. /* From here down, you are not expected to
  11. understand.... for now :)
  12.  
  13.  
  14. Nothing to see here!
  15.  
  16. */
  17.  
  18. // tests
  19.  
  20. function testFunctionWorks(fn, input, expected) {
  21. if (fn(input) === expected) {
  22. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  23. return true;
  24. } else {
  25. console.error(
  26. 'FAILURE: `' +
  27. fn.name +
  28. '([' +
  29. input +
  30. '])` should be ' +
  31. expected +
  32. ' but was ' +
  33. fn(input)
  34. );
  35. return false;
  36. }
  37. }
  38.  
  39. function runTests() {
  40. const input1 = [13, 22, 4, 47, 15, 35, 82];
  41. const result1 = 15;
  42. const input2 = [25, 20, 15, 10, 5];
  43. const result2 = 25;
  44.  
  45. const testResults = [
  46. testFunctionWorks(divisibleBy5, input1, result1),
  47. testFunctionWorks(divisibleBy5, input2, result2),
  48. ];
  49.  
  50. const numPassing = testResults.filter(function(result) {
  51. return result;
  52. }).length;
  53. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  54. }
  55.  
  56. runTests();
Add Comment
Please, Sign In to add comment