I have been able to create the array and average but I am running into roadblocks extracting information from the array.
I need to: Loop and find students that failed. Find the student with the highest mark
JavaScript
x
38
38
1
var numStu = 0;
2
3
numStu = parseInt(prompt("Please enter the number of students enrolled in the class."));
4
5
var stuInfo = Array();
6
stuInfo[0] = Array(); // holds student's names
7
stuInfo[1] = Array(); // holds marks
8
stuInfo[2] = Array();
9
10
for (g = 0; g < numStu; g++) {
11
stuInfo[0][g] = prompt("Enter the name of student number " + g + ".");
12
stuInfo[1][g] = parseFloat(prompt("Enter the Mult 114 grade " + stuInfo[0][g] + "."));
13
14
}
15
16
document.writeln("<table border='1' width='50%'>")
17
document.writeln("<tr><th>Name</th><th>Chem</th></tr>")
18
19
for (row = 0; row < numStu; row++) {
20
document.writeln("<tr>");
21
document.writeln("<td>");
22
document.writeln(stuInfo[0][row]); //Student names
23
document.writeln("</td>");
24
document.writeln("<td>");
25
document.writeln(stuInfo[1][row]); //Student marks
26
document.writeln("</td>");
27
28
}
29
30
document.writeln("</table>")
31
32
var sum = 0;
33
for (mult = 0; mult < numStu; mult++) {
34
sum = sum + stuInfo[1][mult];
35
}
36
var avg1 = sum / numStu; // Calculate chem averages
37
document.writeln("<p>The class average: " + avg1 + " percent.");
38
I was trying to add code as below to extract – but it crashes on me. Any assistance would be greatly appreciated.
JavaScript
1
20
20
1
var gradeVal = 0;
2
3
var gradeIndex = -1;
4
5
6
for (var i = 0; i <= stuInfo.length; i++) {
7
8
if(stuInfo[i] > gradeVal) {
9
10
gradeVal = stuInfo[i];
11
gradeIndex = i;
12
}
13
else {
14
15
}
16
}
17
18
document.writeln("the highest score is " + gradeVal +);
19
document.writeln("the student is " + stuInfo[gradeIndex] +);
20
Thanks
Advertisement
Answer
Maximum value
JavaScript
1
10
10
1
const maxScore = (data) => {
2
let max = data.length > 0 ? data[0] : unedfined;
3
4
for (let i = 1; i < data.length; i++) {
5
if (data[i] > max) max = data[i];
6
}
7
8
return max;
9
};
10
Minimum value
JavaScript
1
10
10
1
const minScore = (data) => {
2
let min = data.length > 0 ? data[0] : unedfined;
3
4
for (let i = 1; i < data.length; i++) {
5
if (data[i] < min) min = data[i];
6
}
7
8
return min;
9
};
10