I recently encountered a simple but interesting problem, the "Grading Students" challenge, and wanted to share how I solved it using JavaScript. The goal of this problem is to apply specific rounding rules to students' grades based on certain conditions.
Problem Breakdown
The task is to round student grades according to the following rules:
Any grade less than 38 remains unchanged because it’s a failing grade.
If the difference between a grade and the next multiple of 5 is less than 3, the grade should be rounded up to the nearest multiple of 5.
If the difference is 3 or greater, the grade remains unchanged.
Example
Let's take a quick example to see how this works:
A grade of
84
will round to85
(because the next multiple of 5 is 85, and the difference is less than 3).A grade of
29
will remain the same (because it’s below 38 and considered a failing grade).A grade of
57
will remain57
(because the next multiple of 5 is 60, and the difference is greater than 3).
Approach to Solve the Problem
To solve this problem, the steps are as follows:
Loop through each grade: Iterate over the list of grades.
Check if the grade is less than 38: If so, skip to the next grade because no rounding is required for failing grades.
Find the next multiple of 5: For each grade, calculate the next multiple of 5.
Check the difference: If the difference between the next multiple of 5 and the current grade is less than 3, round the grade up to that multiple of 5.
Return the modified list of grades: After processing all the grades, return the updated list.
Code Implementation
Here’s the code I wrote to solve this:
function gradingStudents(grades) {
// Loop through all the grades
for (let i = 0; i < grades.length; i++) {
// Calculate the next multiple of 5
let nextMultipleOfFive = Math.ceil(grades[i] / 5) * 5;
// Skip grades less than 38 (failing grades)
if (grades[i] < 38) {
continue;
}
// Round the grade if the difference is less than 3
if (nextMultipleOfFive - grades[i] < 3) {
grades[i] = nextMultipleOfFive;
}
}
return grades;
}
Step-by-Step Walkthrough
Loop through the grades:
The first step is to loop through the list of grades using a simplefor
loop.Calculate the next multiple of 5:
For each grade, I used the formulaMath.ceil(grade / 5) * 5
to find the next multiple of 5. For example:
For a grade of
84
, the next multiple of 5 is85
.For a grade of
73
, the next multiple of 5 is75
.
Check if the grade is less than 38:
Grades below 38 are considered failing, so they do not get rounded. If the grade is less than 38, we simplycontinue
to the next grade.Compare the grade with the next multiple of 5:
If the difference between the grade and the next multiple of 5 is less than 3, we update the grade to that multiple. For instance:
If the grade is
84
, and the next multiple is85
, the difference is 1, so we round84
to85
.If the grade is
73
, and the next multiple is75
, the difference is2
, so we round73
to75
.
- Return the modified grades: After processing all the grades, we return the updated array of grades.
Top comments (0)