Guest User

Untitled

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