Click to See Complete Forum and Search --> : Using Math.max on an array?
spunk33grl
08-13-2004, 01:29 PM
I need to write a function that will take an array of a certain number of values, and then fill it with random values (using the Math.random object). I got that part to work just fine.... The second part I need the function to do is find the max value (and a second function needs to find the min value) from the array. Now I know you can't just use the Math.max object the same way you normally would....but I am so lost as to how to go about using the Math.max object to solve this problem? Any help would be greatly appreciated.
nitwit
08-13-2004, 01:48 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>untitled</title>
</head>
<body>
<pre>
<script type="text/javascript">
Array.prototype.getMax = function()
{
var max = Number.MIN_VALUE, v, len = this.length, i = 0;
for (; i < len; ++i)
if (typeof (v = this[i]) == 'number')
max = Math.max(max, v);
return max;
}
Array.prototype.getMin = function()
{
var min = Number.MAX_VALUE, v, len = this.length, i = 0;
for (; i < len; ++i)
if (typeof (v = this[i]) == 'number')
min = Math.min(min, v);
return min;
}
var x = [22, 33.33, 590, 4, -4503, -4, 'foo', 65.7, 3098.67];
document.writeln('<hr />');
document.writeln('x = [ ' + x + ' ]');
document.writeln('<hr /><br />');
document.writeln('x.getMax() = ' + x.getMax());
document.writeln('<br />');
document.writeln('x.getMin() = ' + x.getMin());
</script>
</pre>
</body>
</html>
neil9999
08-13-2004, 01:59 PM
A is the array.
<script type="text/javascript">
var a=[5,26,4,96,7];
var b=a[0];
var c=a[0];
var d=0;
while(d<a.length){
b=Math.max(b,a[d]);
c=Math.min(c,a[d]);
d++
}
alert("Array: "+a+", Max: "+b+", Min: "+c)
</script>
It works by setting the first item of the array as the max value, then testing it against the secound value, and sets this as the new maxvalue if needbe etc.
Hope this helps,
Neil
spunk33grl
08-13-2004, 02:15 PM
Thanks so much! That was exactly what I needed. I knew that you would need to work it by comparing them two by two, but I wasn't quite sure how to go about doing it.
neil9999
08-13-2004, 02:34 PM
Shorter way:
<script type="text/javascript">
var a=[5,26,4,96,7];
var b=a[0];
var c=a[0];
for(d=0;d<a.length;d++){
b=Math.max(b,a[d]);
c=Math.min(c,a[d]);
}
alert("Array: "+a+", Max: "+b+", Min: "+c)
</script>
Neil
nitwit
08-13-2004, 02:37 PM
Use the two new Array methods above, better solution. Just stick them anywhere. ;)
neil9999
08-13-2004, 02:49 PM
What's better about it?
If it's the return bit:
<script type="text/javascript">
function minmax(a,e){
var b=a[0];
var c=a[0];
for(d=0;d<a.length;d++){
b=Math.max(b,a[d]);
c=Math.min(c,a[d]);
}
return eval(e);
}
</script>
<input type="button" value="Test min" onclick="alert(minmax([5,26,4,96,7],'c'))">
<input type="button" value="Test max" onclick="alert(minmax([5,26,4,96,7],'b'))">
Array: 5,26,4,96,7
'c' the value of the second argument for min, 'b' for max. First argument is array.
This can have more than 1 array on the same page without repeating the function.
Neil