Student Grade Rest Parameter

Student Grade Rest Parameter

Write a simple JavaScript program to calculate the average mark and to calculate the grade of a student. 
Pass the rest parameter prefixed with three dots (…) as an argument to implement the function calculateAverageMark() – calculateAverageMark(…args)
Calculate the average mark of the student and return it. 
Then, this average is used to determine the corresponding grade. 
Return the output with 2 decimal places.

Implement the  calculateStudentGrade() function to calculate the student grade based on the average mark.
The average mark returned by the function calculateAverageMark() is passed as argument to this function.
The grades are computed as follows:

RangeGrade
<50F
<60D
<70C
<80B
<90A
<100O

Note: You may verify the correctness of your function by invoking it from within console.log as shown in sample input and output

Sample Input and Output:
Sample Input1:
console.log(calculateAverageMark(80, 77, 88, 95, 68));
Sample Output1:
81.60

Sample Input2:

var averageMarks= calculateAverageMark(80, 77, 88, 95, 68);
console.log(calculateStudentGrade(averageMarks));
Sample Output2:
A

SOLUTION

Program: Student Grade Rest Parameter Solution in JavaScript

var averageMarks = 0;   // Use this variable to store the average mark 
var studentGrade = "";  // Use this variable to store the student grade 


function calculateAverageMark(...arr)
{

for (var i=0; i < arr.length; i++) {
        averageMarks += arr[i];
        var avg = (averageMarks/arr.length);
}

    return avg.toFixed(2);
}

function calculateStudentGrade(avg)
{

        if (avg < 50){
              return 'F';
          } 
        else if (avg < 60)
         {
            return 'D';
                  } 
        else if (avg < 70) 
             {
                return 'C';
               
        }
        else if (avg < 80) 
        {
                return 'B'; 
        }
        else if (avg < 90)
         {
            return 'A'; 
        }
        
        else if (avg < 100) 
        {
                return 'O';
}
}

Related:

Leave a Comment

thirteen − one =