12
I have a JavaScript array and a variable like so;
var a = [0, 1200, 3260, 9430, 13220],
b = 4500;
What would be the smartest way to select the largest value in the array that's still smaller than or equal to the variable?
In this example, I'd need to select 3260.
I could do something like this;
$.each(a, function(i){
if(a[i] <= b && a[i+1] > b){
var c = a[i];
return false;
}
});
But I'm thinking that might not work if the selected array value is the last one. Not to mention, to me it looks like a lot of code for something rather simple.
Is there a smarter/less verbose way of achieving what I'm after?
(and yes, I know I shouldn't have used a jQuery loop for that but I'm lazy when typing examples)