Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding the maximum number in an array?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am having trouble writing this script so it returns the maximum number in the given array.

Here is the script. The script asks for names and scores for each person. The script is then suppose to return a table with the names and score.. not a problem did that.. then find the average of the scores.. not a problem did that too... Then the script is suppose to find the max score and asociate that with the person's name.

Can anyone help? Thanks.

The max code part is under... function highGrade

<html>
<head>
</title><title>
<script language = &quot;JavaScript1.2&quot;>
<!--

function init()
{

var names = new Array(5);
var scores = new Array(5);

GetArrays( names, scores)
var maxgrade = highGrade(scores)
var averagescore = average( scores )
printArray( names, scores, averagescore, maxgrade)

}

function GetArrays( names, scores)
{
var NumbersNamePeople;
var Cnt;

NumbersNamePeople = parseInt(prompt(&quot;How many names and scores? &quot;,&quot;&quot;));

for(Cnt=0; Cnt < NumbersNamePeople; Cnt++)
{
names[Cnt] = prompt(&quot;Enter the name of person number &quot; + (Cnt + 1) + &quot;: &quot;,&quot;&quot;);
scores[Cnt] = parseFloat(prompt(&quot;Enter &quot; + names[Cnt] + &quot;'s Score: &quot;,&quot;&quot;));
}
}

function average( scores )
{
var Cnt;
var sum ;
var ave;

sum = 0;

for(Cnt = 0; Cnt < scores.length; Cnt++)
{
sum = sum + scores[Cnt];
}
if(scores.length > 0)
{
ave = sum / scores.length;
}
else
{
ave = 0;
}
return ave;
}

function highGrade(scores)
{

var maxgrade;
var Cnt;

maxgrade = 0;

for(Cnt = 0; Cnt <= scores.length; Cnt++)
{
maxgrade = maximum(scores[Cnt])
}
return maxgrade;

}

function printArray( names, scores, averagescore, maxgrade)
{
var win;
var Cnt;

win = open(&quot;&quot;, &quot;displayWindow&quot;, &quot;width=500,height=300,status=yes,toolbar=no,menubar=yes,resizable=yes,scrollbars=yes&quot;);

win.document.writeln('<html>');
win.document.writeln('<head>');
win.document.writeln('<title>Lab 7, Experience 1, Window 2</title>');
win.document.writeln('</head>');
win.document.writeln('<body>')
win.document.writeln('<table border=&quot;4&quot;>');
win.document.writeln('<tr>');
win.document.writeln('<td>Names</td><td>Scores</td>');
win.document.writeln('</tr>');

for(Cnt = 0; Cnt < scores.length; Cnt++)
{

win.document.writeln('<tr>');
win.document.writeln('<td>' + names[Cnt] + '</td><td>' + scores[Cnt] + '</td>');
win.document.writeln('</tr>');

}

win.document.writeln('</table>');

win.document.writeln('<p>The Average of the scores is: <b>'+ Math.round(averagescore) +'</b></p>')
win.document.writeln('<p>The Maximun Score is: '+ maxgrade +'</b></p>')
win.document.writeln('</body>');
win.document.writeln('</html>');
win.document.close();

}

//-->

</script>

</head>

<body>

<p><input type=&quot;button&quot; value=&quot;Button&quot; name=&quot;B3&quot; onclick=&quot;init()&quot;></p>
</form>

</body>

</html>


 
function Array_max()
{var zx,xi,i;xi=this.length;for(i=0;i<xi;i++){
if(i<1){zx=this}else{zx=(zx<this)?this:zx;}}
return zx}
Array.prototype.max=Array_max

this function will return the max numeric value in an array.
Call it like this: highest = myarray.max() jared@aauser.com
 
Dear jaredn,

also this would work... of course only when the array contains numbers. To make the function more 'reusable' you need to check for objects and require the presence of a 'compare' function or something like that. &quot;But, that's just my opinion... I could be wrong&quot;. Anyway...:

function Array_max(){
var nret = 0; // if the array is empty return... what?
for(n=0; n<this.length; n++)
nret = Math.max(nret, this[n]);

return nret;
}
Array.prototype.max = Array_max;

Also, I'm just curios, why is your code devoid of whitespace? Are you concerned with download time? Conserving disk space?

-pete
 
Do either of you know how to associate the Scores Array to the Names Array? So the max position in the Scores array will be associated with the same position in the Names Array?


Thanks
 
as far as the whitespace goes...yeah it looks bad I know. I have *really* tight requirements on download times where I work and every little byte counts...I should add space before posting them here...yes pete from now on I will check for objects also :eek:)...

gblasko:

associated with the same position? I'm not sure what you mean...reqord your ? and I'll see what I can do... jared@aauser.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top