Cityscape
Girl

JavaScript

Exercise: Grade Calculator

Easy
1 h 0 min

Introduction

In this exercise, you will build a simple Grade Calculator that uses variables, functions, conditionals, loops, arrays, and objects. You will create a list of student objects, assign each a grade based on their score, and then calculate and print the average score.

What you need to do

Student Data

Create an array named students using let that contains at least three student objects. Each object should have two properties:

  • name (a string)
  • score (a number)

Grade Assignment

Write a function named assignGrade that takes a student's score as its parameter and returns:

  • "Pass" if the score is greater than or equal to 60
  • "Fail" otherwise

Updating Student Objects

Use a loop to iterate over the students array and add a new property grade to each student object. The value of grade should be determined by the assignGrade function.

Calculating the Average

Write a function named calculateAverage that takes the students array and returns the average score of all students.

Output

Use a loop to print each student's details in the following format:

Finally, print the average score in the format:

Let's get to it!

Grade Calculator

  • Create an array named students using let that contains at least three student objects.
  • Each student object should have the properties name (a string) and score (a number).
  • Create a function named assignGrade(score) that returns "Pass" if the score is greater than or equal to 60, otherwise "Fail".
  • Loop through the students array and add a new property grade to each student object using the assignGrade function.
  • Create a function named calculateAverage(students) that calculates and returns the average score of all students.
  • Use a loop to print each student’s details in the format "<name> - Score: <score>, Grade: <grade>".
  • Finally, print the average score in the format "Average Score: <average>".

hakatemia pro

Ready to become an ethical hacker?
Start today.

As a member of Hakatemia you get unlimited access to Hakatemia modules, exercises and tools, and you get access to the Hakatemia Discord channel where you can ask for help from both instructors and other Hakatemia members.