-2

how can you find the min or max number in a function like this {"a",5,"h",7,true,6,"h",7}. The solution must be applicable to other kinds of functions with the same characteristic. I set min = arguments[0] but what if it's not a number? you know i was reading about functions and I read about arguments objects,which contains an array of arguments.i tried this:

function argsMin() {
  var i = 0;
  var min=arguments[0];
  for(i=0;i<arguments.length;i++) {
    if(min>arguments[i]){
      min=arguments[i];
      }
  }
  return min;   
}

document.write(argsMin(1124,562,-973,955));

now what if the first index is not a number.

arash
  • 11
  • 1
  • 6
  • What have you tried so far, where are you stuck? I assume you came across this question: [Obtain smallest value from array in Javascript?](http://stackoverflow.com/q/8934877/218196) (because that's the first result searching for [`[javascript] min number in array`](http://stackoverflow.com/search?q=%5Bjavascript%5D+min+number+in+array))... do you have troubles adapting it to your situation? – Felix Kling Aug 29 '14 at 14:50
  • If you want to test whether a value is a number or not, you can use the `typeof` operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof – Felix Kling Aug 29 '14 at 15:07
  • it works fine.I don't want the max.but what if the first argument in a function is a string? – arash Aug 29 '14 at 15:53

5 Answers5

2

Sweet and simple:`

var arr = ["a",5,"h",7,true,6,"h",7];
var min = Math.min.apply(null, arr.filter(function(el){
    return typeof el == "number";
}));
//min = 5

And for older browser that do not support filter, use you can use polyfill (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter#Compatibility)

Ovais
  • 374
  • 2
  • 7
0

First for each element check if (typeof element== "number") and then check this to find min JavaScript: min & max Array values?

Community
  • 1
  • 1
koninos
  • 4,969
  • 5
  • 28
  • 47
0

You can apply reduce function on the array to find min and max as followed.

min = array.reduce(function(x,y){if (typeof y === 'number' && (y<x)) return y; return x;}, Infinity)
max = array.reduce(function(x,y){if (typeof y === 'number' && (y>x)) return y; return x;}, -Infinity)

If there is no number in the array, min will contain Infinity and max will contain -Infinity

nisargjhaveri
  • 1,469
  • 11
  • 21
0
  function minOfArray(array) {
    return Math.min.apply(this, array.filter(function (a) { return !isNaN(a); }));
  }

  function maxOfArray(array) {
    return Math.max.apply(this, array.filter(function (a) { return !isNaN(a); }));
  }
Lucas Moeskops
  • 5,445
  • 3
  • 28
  • 42
0

Its actually really simple.

Just drop this inside of a loop:

Math.min(x, y); 

Cycle through all of the elements and keep a variable lowest to hold the one that is lowest. Once all of the elements have been compared your lowest number will be held by the variable.

To determine if it is a number:

isFinite(String(foo).trim() || NaN)

Finally, look up "linear search" if going about this process confuses you.

Linear Search: http://en.wikipedia.org/wiki/Linear_search

Andrew
  • 995
  • 6
  • 10