Grade Calculator
Let's start by defining the function calculateGrade
that takes one parameter score
, which is a number representing a student's score.
function calculateGrade(score) { // Function body will go here };
Next, let's implement the function to return the grade based on the specified criteria. We will use a series of if
, else if
, and else
statements to check the value of score against the grading criteria. Depending on the value of score, the function returns the corresponding grade: "A", "B", "C", "D", or "F".
function calculateGrade(score) { // Determine the grade based on score if (score >= 90) { return "A"; } else if (score >= 80) { return "B"; } else if (score >= 70) { return "C"; } else if (score >= 60) { return "D"; } else { return "F"; } };
We can also use a switch statement to return the grade based on the specified criteria.
function calculateGrade(score) { // Determine the grade based on score using switch statement switch (true) { case (score >= 90): return "A"; case (score >= 80): return "B"; case (score >= 70): return "C"; case (score >= 60): return "D"; default: return "F"; } };