Grading students
Tags: javascript, beginners, programming, tutorial
Published: 2024-03-14 14:21:52

*Prepare your favorite cup of coffee*, because we are about to enter the fantastic world of **Grading students**.

## What is a grading students?

A university has the following grading policy:

Each student receives a grade from 0 to 100.
Any grade below 40 is considered failed.

The teacher rounds each student's grade according to these rules:

- If the difference between the grade and the next multiple of 5 is less than 3, round the grade to the next multiple of 5.
- If the grade value is less than 38, no rounding will occur, as the result will still be a failed grade.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xrybxc2eewnu4lurr4eo.jpeg)

## Examples

- grade = 84 rounded to 85 (85 - 84 is less than 3)
- grade = 29 do not round (the result is less than 40)
- grade = 57 do not round (60 - 57 is equal to 3)

Given the initial grade value for each of the n students, write code to automate the rounding process.

```js
// Input example
[73, 67, 38, 33]

// Output example
[75, 67, 40, 33]
```

## Step by step

The first step is to create the function that will be responsible for rounding the grades. It will be called `gradingStudents`. Next we will initialize an empty array to store the notes after rounding, `roundedNotes`:

```js
// Function to round notes
function gradingStudents(grades) {
    // Starting an empty array to store the rounded notes
    const roundedNotes = [];
}
```

Next we must iterate over the original notes array, the `grades`:

```js
// Iterates over each note
grades.forEach((grade) => {});
```

Now let's determine the next multiple of 5 for each number in `grids`. In this operation we will divide the `grade` by 5 and then apply the `Math.ceil` method to round up to the next integer.

The `*5` guarantees that the resulting number is the next number that is a multiple of 5:

```js
// Calculates the next number that is a multiple of 5
const nextMultipleOfFive = Math.ceil(grade / 5) * 5;
```

Let's compare the difference between `nextMultipleOfFive` and `grade` to see if the difference is less than 3 and greater than or equal to 38:

```js
// If the difference between the next multiple of 5 and the number is less than 3 and the score is greater than or equal to 38
if (nextMultipleOfFive - grade < 3 && grade >= 38) {}
```

If the `grade` satisfies the above conditions, we will collect the `nextMultipleOfFive` in the `roundedNotes` array:

```js
// Consider the next multiple of 5
roundedNotes.push(nextMultipleOfFive);
```

Otherwise, the original note will be stored in the `roundedNotes` array:

```js
else {
    // Otherwise, the original note will be stored in the `roundedNotes` array
    roundedNotes.push(grade);
}
```

After iterating over each note and applying the rounding rules, we have the `roundedNotes` array with the notes properly rounded. So, we just return `roundedNotes`:

```js
// Returns the array with rounded notes
return roundedNotes;
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yoti2segeeb8poqx465.jpeg)

## Final resolution

After following the steps step by step, we have our final resolution:

```js
const grades = [73, 67, 38, 33];

// Function to round notes
function gradingStudents(grades) {
    // Starting an empty array to store the rounded notes
    const roundedNotes = [];

    // Iterates over each note
    grades.forEach((grade) => {
        // Calculates the next number that is a multiple of 5
        const nextMultipleOfFive = Math.ceil(grade / 5) * 5;
        // If the difference between the next multiple of 5 and the number is less than 3 and the score is greater than or equal to 38
        if (nextMultipleOfFive - grade < 3 && grade >= 38) {
            // Consider the next multiple of 5
            roundedNotes.push(nextMultipleOfFive);
        } else {
            // Otherwise, the original note will be stored in the `roundedNotes` array
            roundedNotes.push(grade);
        }
    });

    // Returns the array with rounded notes
    return roundedNotes;
}

console.log(gradingStudents(grades)); // [75, 67, 40, 33]
```

*Share the code, spread knowledge and build the future!* 😉

> Images generated by **DALL·E 3**