Guest User

Untitled

a guest
Apr 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. function max(numbers) {
  2. // your code here
  3. let highestMax = numbers[0];
  4. for (let i = 0; i <numbers.length; i++) {
  5. if(numbers[i] > highestMax) {
  6. highestMax = numbers[i];
  7. }
  8. }
  9. return highestMax;
  10. }
  11.  
  12.  
  13. function min(numbers) {
  14. // your code here
  15. let lowestMin = numbers[0];
  16. for(let i = 0; i < numbers.length; i++) {
  17. if(numbers[i] < lowestMin){
  18. lowestMin = numbers[i];
  19. }
  20. }
  21. return lowestMin;
  22. }
  23.  
  24. /* SOLUTION
  25.  
  26. function max(numbers) {
  27. // we set `currentMax` to the value
  28. // of first item in `numbers`,
  29. // then we loop through `numbers`,
  30. // comparing each item to `currentMax`.
  31. // if the item is greater than `currentMax`,
  32. // we set `currentMax` to that number.
  33. let currentMax = numbers[0];
  34. for (let i = 0; i < numbers.length; i++) {
  35. if (numbers[i] > currentMax) {
  36. currentMax = numbers[i];
  37. }
  38. }
  39. return currentMax;
  40. }
  41.  
  42. function min(numbers) {
  43. // we set `currentMin` to the value
  44. // of first item in `numbers`,
  45. // then we loop through `numbers`,
  46. // comparing each item to `currentMin`.
  47. // if the item is less than `currentMin`,
  48. // we set `currentMin` to that number.
  49. let currentMin = numbers[0];
  50. for (let i = 0; i < numbers.length; i++) {
  51. if (numbers[i] < currentMin) {
  52. currentMin = numbers[i];
  53. }
  54. }
  55. return currentMin;
  56. }
  57.  
  58. */
  59.  
  60. /* From here down, you are not expected to
  61. understand.... for now :)
  62.  
  63.  
  64. Nothing to see here!
  65.  
  66. */
  67.  
  68. // tests
  69.  
  70. function testFunctionWorks(fn, input, expected) {
  71. if (fn(input) === expected) {
  72. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  73. return true;
  74. } else {
  75. console.log(
  76. 'FAILURE: `' +
  77. fn.name +
  78. '([' +
  79. input +
  80. '])` should be ' +
  81. expected +
  82. ' but was ' +
  83. fn(input)
  84. );
  85. return false;
  86. }
  87. }
  88.  
  89. function testEmpty(fn) {
  90. if (fn([]) === null || fn([]) == undefined) {
  91. console.log(`SUCCESS: ${fn.name} works on empty arrays`);
  92. return true;
  93. } else {
  94. console.log(
  95. `FAILURE: ${fn.name} should return undefined or null for empty arrays`
  96. );
  97. return false;
  98. }
  99. }
  100.  
  101. (function runTests() {
  102. // we'll use the variables in our test cases
  103. const numList1 = [-5, 28, 98, -20013, 0.7878, 22, 115];
  104. const realMin1 = numList1[3];
  105. const realMax1 = numList1[6];
  106. const numList2 = [0, 1, 2, 3, 4];
  107. const realMin2 = numList2[0];
  108. const realMax2 = numList2[4];
  109.  
  110. const testResults = [
  111. testFunctionWorks(max, numList1, realMax1),
  112. testFunctionWorks(max, numList2, realMax2),
  113. testFunctionWorks(min, numList1, realMin1),
  114. testFunctionWorks(min, numList2, realMin2),
  115. testEmpty(max),
  116. testEmpty(min),
  117. ];
  118.  
  119. const numPassing = testResults.filter(function(result) {
  120. return result;
  121. }).length;
  122. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  123. })();
Add Comment
Please, Sign In to add comment