2

I have a low temperature array in javascript and I need to get the lowest temp so I used the math.min function. The first time though the low temp is returned and then after that I get NaN.

var lowTempArray = []; 
var lowest = Math.min(lowTempArray);

message += '<tr><td colspan="3"class="alt">The lowest temperature of ' + lowest.toFixed(2) + ' occurred on ' + ((date.getMonth() + 1) + '/' + (date.getDate()) + '/' +  date.getFullYear()) + '.' + '</td></tr>';
charlietfl
  • 170,828
  • 13
  • 121
  • 150
Julia
  • 35
  • 5

3 Answers3

3

Since min wants an argument list, not an array, you can use apply to use an array as the argument list for a function, so for example:

var arr=[4,5,6,3,1,7,8];
Math.min.apply(this,arr); //1

See more on apply: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

chiliNUT
  • 18,989
  • 14
  • 66
  • 106
2

Since Math.min only takes a series of numbers, I'd advise just sorting the array and then taking the first element.

So you could do

lowTempArray.sort(function(a,b){return a-b;});
var lowest = lowTempArray[0];

then use lowest.toFixed(2) in your other statement.

cheers :)

Rooster
  • 9,954
  • 8
  • 44
  • 71
  • Thanks so much! That worked great! Hey, do you know how to get the date for that input? I think I need to use indexOf for the item in the low temp and then somehow call that same indexed number for the date object. I could be completely wrong. – Julia Oct 25 '15 at 22:08
  • Oh, and the number was returned as a string so I had to use parselnt: lowest = parseInt(lowest, 10); – Julia Oct 25 '15 at 22:13
  • im not sure what youre asking in terms of the date. That might be a different question. At the very least you'll need to elaborate. – Rooster Oct 25 '15 at 22:17
  • It is so I will just ask a new question. Thanks! – Julia Oct 25 '15 at 22:21
1

Math.min will want a series of number like Math.min( 1,2,3,4,5 ) and not an array;

You will have to iterate through the array manually like this:

var lowest = Number.MAX_VALUE;
for ( var i = 0; i < lowTempArray.length; i++ ) {
    if ( lowTempArray[i] < lowest ) {
        lowest = lowTempArray[i];
    }
}

Edit: Let me rewrite it into a function:

function lowest( arr ) {
    var lowest = Number.MAX_VALUE;
    for ( var i = 0; i < arr.length; i++ ) {
        if ( arr[i] < lowest ) {
            lowest = arr[i];
        }
    }
    return lowest;
}

Use it like this:

var lowest = lowest( lowTempArray );

Sorting is also a way, as others have suggested, but it might take more CPU-power when going through a long list.

Hasse Björk
  • 1,431
  • 13
  • 19
  • Is there a function you can use to get the min amount out of an array? This array is created though the user's input. – Julia Oct 25 '15 at 21:53
  • The code I wrote above will give set the lowest value in `lowTempArray` in the variable `lowest` – Hasse Björk Oct 25 '15 at 21:54
  • Thanks. Yes the for loop is a good choice and you are probably right about sorting and the CPU stress. – Julia Oct 25 '15 at 22:20