Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="description" content="max/min drill solution">
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width">
  7. <title>max/min drill solution</title>
  8. </head>
  9. <body>
  10. x
  11. <script id="jsbin-javascript">
  12. function max(numbers) {
  13. for (var i=0;i<numbers.length;i++) { //loop through array
  14. var maxNum = 0; //create point of comparison and placeholder
  15. if (maxNum < numbers[i]) { //compare current num to maxNum
  16. maxNum = numbers[i];// if larger, replace maxNum
  17. }
  18. }
  19. return maxNum;
  20. }
  21.  
  22.  
  23. function min(numbers) {
  24. for (var i=0;i<numbers.length;i++) { //loop through array
  25. var minNum = 0; //create point of comparison and placeholder
  26. if (minNum > numbers[i]) { //compare current num to minNum
  27. minNum = numbers[i];// if smaller, replace minNum
  28. }
  29. }
  30. return minNum;}
  31.  
  32.  
  33.  
  34. /* From here down, you are not expected to
  35. understand.... for now :)
  36.  
  37.  
  38. Nothing to see here!
  39.  
  40. */
  41.  
  42.  
  43. // tests
  44.  
  45. function testFunctionWorks(fn, input, expected) {
  46. if (fn(input) === expected) {
  47. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  48. return true;
  49. }
  50. else {
  51. console.log(
  52. 'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
  53. ' but was ' + fn(input)
  54. );
  55. return false;
  56. }
  57. }
  58.  
  59.  
  60. (function runTests() {
  61. // we'll use the variables in our test cases
  62. var numList1 = [-5, 28, 98, -20013, 0.7878, 22, 115];
  63. var realMin1 = numList1[3];
  64. var realMax1 = numList1[6];
  65. var numList2 = [0, 1, 2, 3, 4];
  66. var realMin2 = numList2[0];
  67. var realMax2 = numList2[4];
  68.  
  69. var testResults = [
  70. testFunctionWorks(max, numList1, realMax1),
  71. testFunctionWorks(max, numList2, realMax2),
  72. testFunctionWorks(min, numList1, realMin1),
  73. testFunctionWorks(min, numList2, realMin2),
  74. ];
  75.  
  76. var numPassing = testResults.filter(function(result){ return result; }).length;
  77. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  78. })();
  79. </script>
  80.  
  81.  
  82.  
  83. <script id="jsbin-source-javascript" type="text/javascript">function max(numbers) {
  84. for (var i=0;i<numbers.length;i++) { //loop through array
  85. var maxNum = 0; //create point of comparison and placeholder
  86. if (maxNum < numbers[i]) { //compare current num to maxNum
  87. maxNum = numbers[i];// if larger, replace maxNum
  88. }
  89. }
  90. return maxNum;
  91. }
  92.  
  93.  
  94. function min(numbers) {
  95. for (var i=0;i<numbers.length;i++) { //loop through array
  96. var minNum = 0; //create point of comparison and placeholder
  97. if (minNum > numbers[i]) { //compare current num to minNum
  98. minNum = numbers[i];// if smaller, replace minNum
  99. }
  100. }
  101. return minNum;}
  102.  
  103.  
  104.  
  105. /* From here down, you are not expected to
  106. understand.... for now :)
  107.  
  108.  
  109. Nothing to see here!
  110.  
  111. */
  112.  
  113.  
  114. // tests
  115.  
  116. function testFunctionWorks(fn, input, expected) {
  117. if (fn(input) === expected) {
  118. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  119. return true;
  120. }
  121. else {
  122. console.log(
  123. 'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
  124. ' but was ' + fn(input)
  125. );
  126. return false;
  127. }
  128. }
  129.  
  130.  
  131. (function runTests() {
  132. // we'll use the variables in our test cases
  133. var numList1 = [-5, 28, 98, -20013, 0.7878, 22, 115];
  134. var realMin1 = numList1[3];
  135. var realMax1 = numList1[6];
  136. var numList2 = [0, 1, 2, 3, 4];
  137. var realMin2 = numList2[0];
  138. var realMax2 = numList2[4];
  139.  
  140. var testResults = [
  141. testFunctionWorks(max, numList1, realMax1),
  142. testFunctionWorks(max, numList2, realMax2),
  143. testFunctionWorks(min, numList1, realMin1),
  144. testFunctionWorks(min, numList2, realMin2),
  145. ];
  146.  
  147. var numPassing = testResults.filter(function(result){ return result; }).length;
  148. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  149. })();
  150. </script></body>
  151. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement