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 = "JavaScript1.2">
<!--
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("How many names and scores? ","");
for(Cnt=0; Cnt < NumbersNamePeople; Cnt++)
{
names[Cnt] = prompt("Enter the name of person number " + (Cnt + 1) + ": ",""
scores[Cnt] = parseFloat(prompt("Enter " + names[Cnt] + "'s Score: ","");
}
}
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("", "displayWindow", "width=500,height=300,status=yes,toolbar=no,menubar=yes,resizable=yes,scrollbars=yes"
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="4">');
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="button" value="Button" name="B3" onclick="init()"></p>
</form>
</body>
</html>
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 = "JavaScript1.2">
<!--
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("How many names and scores? ","");
for(Cnt=0; Cnt < NumbersNamePeople; Cnt++)
{
names[Cnt] = prompt("Enter the name of person number " + (Cnt + 1) + ": ",""
scores[Cnt] = parseFloat(prompt("Enter " + names[Cnt] + "'s Score: ","");
}
}
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("", "displayWindow", "width=500,height=300,status=yes,toolbar=no,menubar=yes,resizable=yes,scrollbars=yes"
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="4">');
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="button" value="Button" name="B3" onclick="init()"></p>
</form>
</body>
</html>