Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
Create an array of at least 4 objects named students

I need help to output each student name and percent on a HTML page and print the grade of students based on the conditions in the loop. Please let me know how to display the results as Name,Percent,Grade for each student in the array. [1]: https://i.stack.imgur.com/s1ewS.png

[enter image description here][1]

let students = [
    {
    studentName: "Daniel Nate",
    marksObtained: 15,
    totalMarks: 25
    },

    {
    studentName: "Robert Lee",
    marksObtained: 80,
    totalMarks: 100
    },

    {
        studentName: "Jennifer Lawrence",
        marksObtained: 58.5,
        totalMarks: 65
    },

    {
        studentName: "Steve Nath",
        marksObtained: 50,
        totalMarks: 100
    },
]

// Use .forEach() method to add a property "percentage" to each student object.
students.forEach(student =>{
    let percentageResult = (student.marksObtained / student.totalMarks) * 100;
    student.score = percentageResult;
});

//Use .sort() method to sort array from highest percentage to lowest
students.sort(function (a, b) {
    return b.score - a.score;
});

//Print results on console
console.log(JSON.stringify(students));

students.forEach(student => {

    let grade = student.score;
    

    if (grade >=90) {
    percentageResult = "Grade A";
}
    else if (grade >=80) {
    percentageResult = "Grade B"
}
    else if (grade >=70) {
    percentageResult = "Grade B+"
}
    else if (grade >=60) {
    percentageResult = "Grade C"
}
    else if (grade >=50) {
    percentageResult = "Grade D"
}
    else if (grade <50) {
    percentageResult = "FAIL"
}
    else {
    percentageResult = "Not a valid Grade";
}

});


document.write("Student Grade:".bold() + percentageResult);

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I believe this is what you were trying to accomplish:

let students = [
    {
        studentName: "Daniel Nate",
        marksObtained: 15,
        totalMarks: 25
    },

    {
        studentName: "Robert Lee",
        marksObtained: 80,
        totalMarks: 100
    },

    {
        studentName: "Jennifer Lawrence",
        marksObtained: 58.5,
        totalMarks: 65
    },

    {
        studentName: "Steve Nath",
        marksObtained: 50,
        totalMarks: 100
    },
]

students.forEach(student => {
  
    let percentageResult = (student.marksObtained / student.totalMarks) * 100;
    let grade = percentageResult;
    
    if(grade){
      if (grade >=90) {
      percentageResult = "A";
    }
    else if (grade >=80) {
      percentageResult = "B"
    }
    else if (grade >=70) {
      percentageResult = "B+"
    }
    else if (grade >=60) {
      percentageResult = "C"
    }
    else if (grade >=50) {
      percentageResult = "D"
    }
    else if (grade <50) {
      percentageResult = "FAIL"
    }
    else {
        document.write(grade)
      }
    }
  document.write("Student Name: " + student.studentName + "<br/>Percentage: ".bold() + grade + "<br/>Grade: ".bold() + percentageResult);
  document.write("<br/><br/>")
  });

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!