Guest User

Untitled

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