Advertisement
dimipan80

Find Min and Max Number

Nov 10th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a JavaScript function findMinAndMax(arr) that accepts as parameter an array of numbers.
  2. The function finds the minimum and the maximum number. Write a JS program minMaxNumbers.js
  3. that invokes your function with the sample input data below and prints the output at the console. */
  4.  
  5. "use strict";
  6.  
  7. var result = {};
  8. function findMinAndMax(arr) {
  9.     arr = arr.sort(function(a, b) {
  10.         return a - b;
  11.     });
  12.  
  13.     result = {
  14.         'Min': arr[0],
  15.         'Max': arr[arr.length - 1]
  16.     };
  17. }
  18.  
  19. findMinAndMax([1, 2, 1, 15, 20, 5, 7, 31]);
  20. console.log('Min -> ' + result.Min);
  21. console.log('Max -> ' + result.Max);
  22.  
  23. findMinAndMax([2, 2, 2, 2, 2]);
  24. console.log('Min -> ' + result.Min);
  25. console.log('Max -> ' + result.Max);
  26.  
  27. findMinAndMax([500, 1, -23, 0, -300, 28, 35, 12]);
  28. console.log('Min -> ' + result.Min);
  29. console.log('Max -> ' + result.Max);
  30.  
  31. findMinAndMax([0.3, 5.4, 12.6, 8.5, 2.4, 1.8, 3.6, 10.7, 1.1, 2, 7.2, 0, -0.9, -1.6, 10]);
  32. console.log('Min -> ' + result.Min);
  33. console.log('Max -> ' + result.Max);
  34.  
  35. findMinAndMax([]);
  36. console.log('Min -> ' + result.Min);
  37. console.log('Max -> ' + result.Max);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement