Guest User

Untitled

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