DEV Community

David
David

Posted on

Solving the HackerRank"Grading Students" Problem in TypeScript

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:

  1. Any grade less than 38 remains unchanged because it’s a failing grade.

  2. 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.

  3. 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 to 85 (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 remain 57 (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:

  1. Loop through each grade: Iterate over the list of grades.

  2. Check if the grade is less than 38: If so, skip to the next grade because no rounding is required for failing grades.

  3. Find the next multiple of 5: For each grade, calculate the next multiple of 5.

  4. 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.

  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;
}
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Walkthrough

  1. Loop through the grades:
    The first step is to loop through the list of grades using a simple for loop.

  2. Calculate the next multiple of 5:
    For each grade, I used the formula Math.ceil(grade / 5) * 5 to find the next multiple of 5. For example:

  • For a grade of 84, the next multiple of 5 is 85.

  • For a grade of 73, the next multiple of 5 is 75.

  1. 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 simply continueto the next grade.

  2. 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 is 85, the difference is 1, so we round 84 to 85.

  • If the grade is 73, and the next multiple is 75, the difference is 2, so we round 73 to 75.

  1. Return the modified grades: After processing all the grades, we return the updated array of grades.

Top comments (0)