Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a JavaScript function findMinAndMax(arr) that accepts as parameter an array of numbers.
- The function finds the minimum and the maximum number. Write a JS program minMaxNumbers.js
- that invokes your function with the sample input data below and prints the output at the console. */
- "use strict";
- var result = {};
- function findMinAndMax(arr) {
- arr = arr.sort(function(a, b) {
- return a - b;
- });
- result = {
- 'Min': arr[0],
- 'Max': arr[arr.length - 1]
- };
- }
- findMinAndMax([1, 2, 1, 15, 20, 5, 7, 31]);
- console.log('Min -> ' + result.Min);
- console.log('Max -> ' + result.Max);
- findMinAndMax([2, 2, 2, 2, 2]);
- console.log('Min -> ' + result.Min);
- console.log('Max -> ' + result.Max);
- findMinAndMax([500, 1, -23, 0, -300, 28, 35, 12]);
- console.log('Min -> ' + result.Min);
- console.log('Max -> ' + result.Max);
- 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]);
- console.log('Min -> ' + result.Min);
- console.log('Max -> ' + result.Max);
- findMinAndMax([]);
- console.log('Min -> ' + result.Min);
- console.log('Max -> ' + result.Max);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement