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
var numStu = 0; numStu = parseInt(prompt("Please enter the number of students enrolled in the class.")); var stuInfo = Array(); stuInfo[0] = Array(); // holds student's names stuInfo[1] = Array(); // holds marks stuInfo[2] = Array(); for (g = 0; g < numStu; g++) { stuInfo[0][g] = prompt("Enter the name of student number " + g + "."); stuInfo[1][g] = parseFloat(prompt("Enter the Mult 114 grade " + stuInfo[0][g] + ".")); } document.writeln("<table border='1' width='50%'>") document.writeln("<tr><th>Name</th><th>Chem</th></tr>") for (row = 0; row < numStu; row++) { document.writeln("<tr>"); document.writeln("<td>"); document.writeln(stuInfo[0][row]); //Student names document.writeln("</td>"); document.writeln("<td>"); document.writeln(stuInfo[1][row]); //Student marks document.writeln("</td>"); } document.writeln("</table>") var sum = 0; for (mult = 0; mult < numStu; mult++) { sum = sum + stuInfo[1][mult]; } var avg1 = sum / numStu; // Calculate chem averages document.writeln("<p>The class average: " + avg1 + " percent.");
I was trying to add code as below to extract – but it crashes on me. Any assistance would be greatly appreciated.
var gradeVal = 0; var gradeIndex = -1; for (var i = 0; i <= stuInfo.length; i++) { if(stuInfo[i] > gradeVal) { gradeVal = stuInfo[i]; gradeIndex = i; } else { } } document.writeln("the highest score is " + gradeVal +); document.writeln("the student is " + stuInfo[gradeIndex] +);
Thanks
Advertisement
Answer
Maximum value
const maxScore = (data) => { let max = data.length > 0 ? data[0] : unedfined; for (let i = 1; i < data.length; i++) { if (data[i] > max) max = data[i]; } return max; };
Minimum value
const minScore = (data) => { let min = data.length > 0 ? data[0] : unedfined; for (let i = 1; i < data.length; i++) { if (data[i] < min) min = data[i]; } return min; };