3

So I try to find the minimum and maximum if an array and want to call the function. I made this code, but it doesn´t work. I just don´t get why.

function getMinMax(arr){
  let maximum = Math.max(arr);
  let minimum = Math.min(arr);
 let result =  ([maximum, minimum]); 
  return result;
};

getMinMax([10,3,8,1,33])
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
thomalex
  • 59
  • 1
  • 2
  • 8
  • 3
    just have a look here: [spread syntax `...`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) – Nina Scholz Jul 15 '20 at 08:51
  • The reason why you code doesn't work is because both [`Math.min`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min) and [`Math.max`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max) expect separate arguments, not a single array argument. eg `Math.max(10,3,8,1,33)` is valid `Math.max([10,3,8,1,33])` isn't. – 3limin4t0r Jul 15 '20 at 08:59
  • 1
    Does this answer your question? [Find the min/max element of an Array in JavaScript](https://stackoverflow.com/questions/1669190/find-the-min-max-element-of-an-array-in-javascript) – Sergey Shubin Jul 15 '20 at 09:02

6 Answers6

5

You can find the maximum and minimum in 2 ways. First way You go on with maximum and minimum form Math function in javascript, if you need to be used when you have to set numbers from max and min, like this :

let maximum = Math.max(10, 3, 8, 1, 33);
let minimum = Math.min(10, 3, 8, 1, 33);

Now you have an array so you have to convert to As above, for example : let array = [1,2,3,4,5] when you use ... before array you convert array to this : 1,2,3,4,5 for example :

function getMinMaxWithMath(arr){
  // Math.max(10,3,8,1,33)
  let maximum = Math.max(...arr);
  // Math.min(10,3,8,1,33)
  let minimum = Math.min(...arr);
 let result =  ([maximum, minimum]); 
  return result;
};

console.log('getMinMaxWithMath ', getMinMaxWithMath([10,3,8,1,33]));

The second way you can use for loop for example :

function getMinMaxForLoop(arr){
  let maximum = arr[0];
  let minimum = arr[0];
  for (let i = 0 ; i < arr.length; i++) {
    if (maximum < arr[i]) {
      maximum = arr[i];
    } else {
      minimum = arr[i];
    }
    
  }
 let result =  ([maximum, minimum]); 
  return result;
};
console.log('getMinMaxForLoop : ',getMinMaxForLoop([10,3,8,1,33]))

You can Look output from this link;

nima amr
  • 603
  • 1
  • 6
  • 13
  • 1
    *"The second way you can use for loop"* you then show a code block without for-loop. This answer is pretty good, but need some better consistency. I would also recommend to build your running examples in an [snippet](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do), instead of linking to an external resource. – 3limin4t0r Jul 15 '20 at 09:51
  • Just an FYI: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max says "both spread (...) and apply will either fail or return the wrong result if the array has too many elements, because they try to pass the array elements as function parameters." – Stonetip Apr 23 '22 at 02:23
3

You try to pass array, but functions (min, max) accept only number arguments.

You need to unpack array to array of arguments with the spread operator (...):

const arr = [10, 3, 8, 1, 33];
const min = Math.min(...arr);
const max = Math.max(...arr);

So, your code should be like this:

function getMinMax(arr){
  let maximum = Math.max(...arr);
  let minimum = Math.min(...arr);
 let result =  ([maximum, minimum]); 
  return result;
};

getMinMax([10,3,8,1,33])
D. Pardal
  • 6,173
  • 1
  • 17
  • 37
  • 1
    *"Just use Math.min/max functions"* isn't the best explanation. The question already uses the `Math.min` and `Math.max` functions (in a wrong way). An answer should explain what went wrong in the question, and how this is solved in your answer. – 3limin4t0r Jul 15 '20 at 09:04
1

If you want to use this method, you need to structure the array

function getMinMax(arr) {
    let maximum = Math.max(...arr);
    let minimum = Math.min(...arr);
    let result = [maximum, minimum];
    return result;
};

getMinMax([10, 3, 8, 1, 33]);
iopzhu
  • 149
  • 5
0

const getMinMax = (arr) => {
  const sortedArr = arr.sort((a,b) => a - b);
  const min = sortedArr[0];
  const max = sortedArr[sortedArr.length - 1];
  return [min, max];
};

console.log(getMinMax([10,3,8,1,33]))

You also can use Array.sort() to achieve your desired result.

Nicolae Maties
  • 2,476
  • 1
  • 16
  • 26
0

You can also try this simple and understandable math

Example

function max(arr) {
  let i;
  let max = arr[0]
  let min = arr[0]
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] > max) {
      max = arr[i]
    } else if (arr[i] < min) {
      min = arr[i]
    }
  }
  let res = (["min: " + min, "max: " + max])
  return res
}

console.log(max([10, 3, 8, 1, 33]))
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
0

Or, as a one-liner (nothing new, only more compact):

const getMinMax=arr=>[Math.min(...arr),Math.max(...arr)];

console.log(getMinMax([10,3,8,1,33]))
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43