Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. //*************MAX MIN************
  2. function max(numbers) {
  3. // your code here
  4. var maxNum = numbers [0];
  5. for (i=1; i<numbers.length; i++){
  6. if (maxNum < numbers [i]){
  7. maxNum = numbers [i];
  8. }
  9. }
  10. return maxNum;
  11. }
  12.  
  13.  
  14. function min(numbers) {
  15. // your code here
  16. var minNum = numbers [0];
  17. for (i=1; i<numbers.length; i++){
  18. if (minNum > numbers [i]){
  19. minNum = numbers [i];
  20. }
  21. }
  22. return minNum;
  23. }
  24.  
  25.  
  26.  
  27. /* From here down, you are not expected to
  28. understand.... for now :)
  29.  
  30.  
  31. Nothing to see here!
  32.  
  33. */
  34.  
  35.  
  36. // tests
  37.  
  38. function testFunctionWorks(fn, input, expected) {
  39. if (fn(input) === expected) {
  40. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  41. return true;
  42. }
  43. else {
  44. console.log(
  45. 'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
  46. ' but was ' + fn(input)
  47. );
  48. return false;
  49. }
  50. }
  51.  
  52.  
  53. (function runTests() {
  54. // we'll use the variables in our test cases
  55. var numList1 = [-5, 28, 98, -20013, 0.7878, 22, 115];
  56. var realMin1 = numList1[3];
  57. var realMax1 = numList1[6];
  58. var numList2 = [0, 1, 2, 3, 4];
  59. var realMin2 = numList2[0];
  60. var realMax2 = numList2[4];
  61.  
  62. var testResults = [
  63. testFunctionWorks(max, numList1, realMax1),
  64. testFunctionWorks(max, numList2, realMax2),
  65. testFunctionWorks(min, numList1, realMin1),
  66. testFunctionWorks(min, numList2, realMin2),
  67. ];
  68.  
  69. var numPassing = testResults.filter(function(result){ return result; }).length;
  70. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
  71. })();
  72.  
  73.  
  74.  
  75. //***************AVERAGE*******************
  76.  
  77. function average(numbers) {
  78. // your code goes here
  79. var numSum = 0;
  80.  
  81. for (i=0; i<numbers.length; i++) {
  82. numSum += numbers[i];
  83. }
  84. return numSum/numbers.length;
  85.  
  86. }
  87.  
  88.  
  89. /* From here down, you are not expected to
  90. understand.... for now :)
  91.  
  92.  
  93. Nothing to see here!
  94.  
  95. */
  96.  
  97.  
  98. // tests
  99.  
  100. function testFunctionWorks(fn, input, expected) {
  101. if (fn(input) === expected) {
  102. console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
  103. return true;
  104. }
  105. else {
  106. console.log(
  107. 'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
  108. ' but was ' + fn(input)
  109. );
  110. return false;
  111. }
  112. }
  113.  
  114. (function runTests() {
  115. var numList1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  116. var correctAns1 = 5.5;
  117. var numList2 = [0, -1, 1];
  118. var correctAns2 = 0;
  119.  
  120. var testResults = [
  121. testFunctionWorks(average, numList1, correctAns1),
  122. testFunctionWorks(average, numList2, correctAns2)
  123. ];
  124. var numPassing = testResults.filter(function(result){ return result; }).length;
  125. console.log(numPassing + ' out of ' + testResults.length + ' tests passing.')
  126. })();
  127.  
  128.  
  129. //****************FIZZBUZZ******************
  130. function fizzBuzz(countTo) {
  131. // your code here
  132. var finalArray = [];
  133.  
  134. for (i=1; i<=countTo; i++) {
  135. if (i%3===0 && i%5===0){
  136. finalArray.push("fizzbuzz");
  137. }
  138. else if (i%3===0){
  139. finalArray.push("fizz");
  140. }
  141. else if (i%5===0){
  142. finalArray.push("buzz");
  143. }
  144. else {
  145. finalArray.push(i);
  146. }
  147. }
  148.  
  149.  
  150. return finalArray;
  151. }
  152.  
  153.  
  154.  
  155.  
  156. /* From here down, you are not expected to
  157. understand.... for now :)
  158.  
  159.  
  160. Nothing to see here!
  161.  
  162. */
  163.  
  164.  
  165.  
  166. // tests
  167. (function testFizzBuzz() {
  168. // we'll use the variables in our test cases
  169. var countTo = 16;
  170. var expected = [
  171. 1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz',
  172. 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz', 16
  173. ];
  174.  
  175. var actual = fizzBuzz(countTo) || [];
  176.  
  177. if (
  178. expected.length === actual.length &&
  179. expected.every(function(item, index) {
  180. return actual[index] === item;}) ) {
  181.  
  182. console.log('SUCCESS: fizzBuzz is working');
  183. }
  184. else {
  185. console.log('FAILURE: fizzBuzz is not working');
  186. }
  187. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement