You've shown:
res = "<?php echo $timings;?>"
res.sort(function(a, b){return b-a});
That would be a string by the time it gets to JavaScript. If what the browser sees is:
res = "10, 5, 19, 2, 1, 32, 3"
res.sort(function(a, b){return b-a});
...then the first step is to turn that string into an array, which you can do like this:
var a = res.split(/[,\s]+/);
Then, just loop through the array and look:
var lowest;
a.forEach(function(num) {
if (num < 6 && (lowest === undefined || lowest > num)) {
lowest = num;
}
});
That's using ES5's forEach
, which nearly all environments now have (but IE8 doesn't). Details on how to loop through arrays can be found in this other Stack Overflow question and its answers.
Or if you need to know the index of the lowest number:
var a = [10, 5, 19, 2, 1, 32, 3];
var lowest, lowestIndex = -1;
a.forEach(function(num, index) {
if (num < 6 && (lowest === undefined || lowest > num)) {
lowest = num;
lowestIndex = index;
}
});