1

Possible Duplicate:
JavaScript: min & max Array values?

var test = [-42, 0, 8];
var biggest=-Infinity;
for (i=0; i<test.length;i++)
{if (test[i]>biggest)
    alert('It is!');
  biggest = test[i];
 else
     alert("It isn't!");}
alert("The biggest element is:" + biggest);

I'm trying to write a program to find the biggest number in an array but my code is not working. Any help please?

Community
  • 1
  • 1
methuselah
  • 11,964
  • 41
  • 148
  • 269

5 Answers5

8

You have been bitten by the "too few braces" bug:

if (test[i]>biggest)
    alert('It is!');
    biggest = test[i]; // THIS IS NOT INSIDE THE IF!

If you fix this, it works just fine.

Of course, you can do this much easier using Math.Max, as this MDN documentation sample shows:

function getMaxOfArray(numArray) {
  return Math.max.apply(null, numArray);
}
Jon
  • 413,451
  • 75
  • 717
  • 787
  • This is why I *always* require braces on conditionals... +1 –  Oct 23 '11 at 23:25
  • 1
    @pst: It's one of those "yeah I know you can do this without braces, but shut up and just use them anyway" issues for me too. – Jon Oct 23 '11 at 23:28
4

This is a bit clever, but quite succinct and works well.

Just call apply() on Math.max() and pass the array as the arguments.

var maxValue = Math.max.apply(null, arr);

jsFiddle.

alex
  • 460,746
  • 196
  • 858
  • 974
1

Although you should listen to @alex, this is how you would have done it if there were no Math.max, and you wanted to code C-style:

var test = [-42, 0, 8];

var biggest = -Infinity;

for(var i = 0; i < test.length; ++i)
    if(test[i] > biggest)
        biggest = test[i];

alert("The biggest element is:" + biggest);
Mateen Ulhaq
  • 21,459
  • 16
  • 82
  • 123
1

An alternative solution

function nsort(a,b){
    return a - b;
}
var test = [-42, 0, 8, 3, 15, 12],
    max  = test.sort(nsort).pop(); // 15

Demo: http://jsfiddle.net/TkeHb/

AlienWebguy
  • 75,484
  • 17
  • 116
  • 141
1

ECMAScript 5th Edition introduces a few higher-order functions for arrays -- yay!.

One is Array.reduce (also known as left fold):

Thus, this can be solved as:

var arr = [-42, 0, 8]
var res = arr.reduce(function (prev,cur) {
   return cur > prev ? cur : prev
}, -Infinity)
res // 8

Not nearly as "elegant" in this case as applying the array as the parameters to Math.max, but higher-order functions can make good additions to a "toolbox" ;-) Consider the slight modification to find the longest string in an array:

var arr = ["Hello", "World!!!", "Wut?"]
var res = arr.reduce(function (prev,cur) {
   return cur.length > prev.length ? cur : prev
}, "")
res // "World!!!"

Try that with Math.max or Array.sort and index/pop ;-)

Happy coding.


While the above is for 5th Edition, a trivial reduce function can be written for ECMAScript 3rd Edition (aka JavaScript 1.5) and equivalent functionality is part of a number of libraries:

function reduce (arr, fn, v) {
  for (var i = 0; i < arr.length; i++) {
    v = fn(v, arr[i])
  }
  return v
}
// used as:
reduce(arr, function (prev,cur) { ... }, initValue)

(This could be added to the Array prototype, but I consider mucking with core prototypes fairly ugly.)