Guest User

Untitled

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