0

Is there a way to compare an integer against an array of integers? For instance, to determine if an int is larger than any of the array ints?

var array = [1, 2, 3, 4];
if(5 > array){ 
    // do something 
}

Update: I guess I meant, is 5 larger than the largest number in the array. Thanks!

2
  • 1
    Judging from your choice of example, I assume you mean "larger than all of the array ints"? Or did you really mean "any"? Commented Jun 6, 2012 at 23:36
  • @MarkByers. Yeah I think you're right... Commented Jun 6, 2012 at 23:40

3 Answers 3

8

You can use Math.max and apply

if (5 > Math.max.apply(Math, array)) {
    // do something
}

Update: To explain as it works. It's described in the docs I linked but I will try to be more clear here:

Math.max returns the largest of zero or more numbers, so:

Math.max(1, 2, 3, 4) // returns 4

apply calls a function with a given 'this' value (the first argument) and arguments provided as an array (the second). so:

function sum(a, b) {
    return a + b;
}

console.log(sum.apply(window, [2, 3])); // 5

Therefore, if you have an array of integers and you want to get the max, you can combine them to have:

console.log(Math.max.apply(Math, [1, 2, 3, 4])); // 4

Because it's exactly like have:

console.log(Math.max(1, 2, 3, 4));

The difference is you pass an array instead.

Hope it's more clear now!

Sign up to request clarification or add additional context in comments.

6 Comments

@gdoron It should :) Which error do you have, and in which browser / js engine have you tested?
Good to hear! Thanks for the jsfiddle link :) (and the plus)
In the explaination I had a typo that I tried to fix but @davin was faster than me ;)
I got confused because of the Math in the apply. You can use this instead: console.log(Math.max.apply(null, [1, 2, 3, 4])); // 4
@gdoron, in that case won't be exactly the equivalent and error prone by some javascript implementation. In your case you made the equivalent of var max = Math.max; console.log(max(1, 2, 3, 4)); but some implementation could expect that this (the context object) is Math and in your case it isn't. For example, in Firefox is perfectly valid have console.log.apply(null, [1, 2]); but it Chrome will raise an error unless you have console.log.apply(console, [1, 2]);. So in order to avoid possible error (now or in the future) is preferable use the exact equivalent.
|
1

There is no good and readable built in way of doing it, but it can be done simply with:

var bigger = true;
for (var i =0; i < array.length; i++) {
    if (5 <= array[i]) {
        bigger = false;
        // you can add here : break;
    }
}

Comments

0

Sure, you could sort the array, take the last element, and see if your integer is greater than that. Or, you could loop through and check each one. Up to you. The loop is a more performant way.

//maybe check the bounds on this if you know it can be a blank array ever
var max = myArray[0];
for(var x = 0; x < myArray.length; x++) {
    max = Math.max(max, myArray[x]);
}

if(500 > max) {
    //do something
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.