Home > other >  I tried to calculate grade and print in table using JavaScript
I tried to calculate grade and print in table using JavaScript

Time:11-24

`I want to write program which calculate students grade according to their marks and then it print in table.

I am learning JavaScript, so i want to do some practice task,, but I'm stuck in between this. I used array object and foreach to print calculated grades in table but it didn't execute.

enter code here

const grade = student.filter(function (object, index, array) {



    if (grade < 60) {

        document.write('Grade is F.');

    }

    else if (grade >= 60 && grade < 70) {

        document.write('Grade is D.');

    }

    else if (grade >= 70 && grade < 80) {

        document.write('Grade is C.');

    }

    else if (grade >= 80 && grade < 90) {

        document.write('Grade is B.');

    }

    else if (grade >= 90 && grade < 100) {

        document.write('Grade is A.');

    }

    return object.grade();

})



student.forEach(function(value, index, array) {

    document.getElementById('-name').innerHTML = value.name;

    document.getElementById('-marks').innerHTML = value.marks;

    document.getElementById('-grade').innerHTML = value.grade;

})
    

CodePudding user response:

// assume you have a student arr looks like this:
const students = [
    {sid: 'Y100001', name: 'Student A', marks: 10},
    {sid: 'Y100002', name: 'Student B', marks: 90},
    {sid: 'Y100003', name: 'Student C', marks: 20},
    {sid: 'Y100004', name: 'Student D', marks: 80},
    {sid: 'Y100005', name: 'Student E', marks: 30},
    {sid: 'Y100006', name: 'Student F', marks: 70},
    {sid: 'Y100007', name: 'Student G', marks: 40},
    {sid: 'Y100008', name: 'Student H', marks: 60},
    {sid: 'Y100009', name: 'Student I', marks: 50},
    {sid: 'Y100010', name: 'Student J', marks: 40},
  ];

// single-line if statement don't need {}.
// use return to end a function or array method can simplify nested if...else statements.
// this functin check the value of a provided number 'marks', and return a string value as the grade.
const getGrade = (marks) => {
  if (marks >= 90) return 'A';
  if (marks >= 80) return 'B';
  if (marks >= 70) return 'C';
  if (marks >= 60) return 'D';
  return 'F';
}

// use a for...of loop to iterate the 'students' array and set the 'grade' porperty of each student according to their 'marks' value.
for (const student of students) student.grade = getGrade(student.marks);

students.forEach(student => {
  console.log(
    `Student name: ${student.name}.`,
    `\n> Mark scored: ${student.marks}.`,
    `\n> Grade: ${student.grade}`
  );
});

/** output:
Student name: Student A. 
> Mark scored: 10. 
> Grade: F
Student name: Student B. 
> Mark scored: 90. 
> Grade: A
Student name: Student C. 
> Mark scored: 20. 
> Grade: F
Student name: Student D. 
> Mark scored: 80. 
> Grade: B
Student name: Student E. 
> Mark scored: 30. 
> Grade: F
Student name: Student F. 
> Mark scored: 70. 
> Grade: C
Student name: Student G. 
> Mark scored: 40. 
> Grade: F
Student name: Student H. 
> Mark scored: 60. 
> Grade: D
Student name: Student I. 
> Mark scored: 50. 
> Grade: F
Student name: Student J. 
> Mark scored: 40. 
> Grade: F
*/

  • Related