In: Computer Science
Design and implement a JavaScript program to accomplish the following: 1. Iterate and accept N test scores and N student names (where 10 <= N <= 20). 2. Compute the sum and average of the scores (display these values). 3. Implement a function that determines the highest score (display this value). 4. Implement a function that determines the lowest score (display this value). 5. Implement a function that determines the letter grade of each score: 90-100 (A); 80 - 89 (b); 70 - 79 (C); 60 - 69 (D); 0 - 59 (F) 6. Display a table showing each student's name, score, and letter grade. 7. The highest and lowest scores should be highlighted with different colors. Complete the determineGrade function in the sample solution using if statements - not switch statement.
This is the 4th time im posting this since every answer im getting is not working
// #2 Finding sum of all scores
function sum(marks_array){
var sum = 0;
for( var i = 0; i < marks_array.length; i++
){
sum += parseInt( marks_array[i], 10 );
}
var avg = sum/marks_array.length;
//document.write( "The sum of all the elements is: " +
sum);
return sum;
}
// #2 finding average of scores
function avg(marks_array){
var sm = sum(marks_array);
var avg = sm/marks_array.length;
//document.write("Avg: "+avg);
return avg;
}
// #3 finding highest score
function highest(marks_array){
var max = Number.NEGATIVE_INFINITY;
for( var i = 0; i < marks_array.length; i++
){
if(max < parseInt(marks_array[i],10)){
max = marks_array[i]
}
}
//document.write(max);
return max;
}
// #4 finding lowest score
function lowest(marks_array){
var min = Number.POSITIVE_INFINITY;
for( var i = 0; i < marks_array.length; i++
){
if(min > parseInt(marks_array[i],10)){
min = marks_array[i]
}
}
//document.write(min);
return min;
}
// #5 Displace letter grade as per the marks
function letterGrade(marks){
marks = parseInt(marks);
var grade;
if(90<=marks){
grade = 'A';
}else if(80<=marks && marks<=89){
grade = 'B';
}else if(70<=marks && marks<=79){
grade = 'C';
}else if(60<=marks && marks<=69){
grade = 'D';
}else{
grade = 'F';
}
//document.write(grade);
return grade;
}
// # displaying grades in table formate
function createGradeTable(students, marks){
var html = "<table><tr
bgcolor='#AFAFA8'>";
html +=
"<td><b>S.No.</b></td>";
html +=
"<td><b>Name</b></td>";
html +=
"<td><b>Marks</b></td>";
html +=
"<td><b>Grade</b></td></tr>";
for(var i=0; i< students.length; i++){
if(highest(marks)==marks[i]){ // if
marks is highest in the class background color is greenish
html += "<tr
bgcolor='#33FF9F'>"
}else
if(lowest(marks)==marks[i]){ // if marks is lowest in
the class background color is reddish
html += "<tr
bgcolor='#FF5733'>"
}else{
// general background color is light
yellow
html += "<tr
bgcolor='#FFFFEC'>"
}
html +=
"<td>"+(i+1)+"</td>";
html += "<td
color='red'>"+students[i]+"</td>";
html +=
"<td>"+marks[i]+"</td>";
html +=
"<td>"+letterGrade(marks[i])+"</td></tr>";
}
html += "</tr></table>";
return html;
}