最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Finding min and max in array Javascript - Stack Overflow

programmeradmin5浏览0评论

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])

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])
Share Improve this question edited Aug 9, 2020 at 11:47 Umutambyi Gad 4,1013 gold badges20 silver badges40 bronze badges asked Jul 15, 2020 at 8:50 thomalexthomalex 691 gold badge4 silver badges8 bronze badges 3
  • 3 just have a look here: spread syntax ... – Nina Scholz Commented Jul 15, 2020 at 8:51
  • The reason why you code doesn't work is because both Math.min and 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 Commented Jul 15, 2020 at 8:59
  • 1 Does this answer your question? Find the min/max element of an Array in JavaScript – Sergey Shubin Commented Jul 15, 2020 at 9:02
Add a ment  | 

6 Answers 6

Reset to default 5

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])

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;

It's just ...

  Math.max(...arrayName)

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]);

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.

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

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

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

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]))

发布评论

评论列表(0)

  1. 暂无评论