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.
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.
// 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
:
// 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
:
// 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:
// 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:
// 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:
// Consider the next multiple of 5
roundedNotes.push(nextMultipleOfFive);
Otherwise, the original note will be stored in the roundedNotes
array:
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
:
// Returns the array with rounded notes
return roundedNotes;
Final resolution
After following the steps step by step, we have our final resolution:
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
Top comments (6)
This grading system looks very useful, especially when it comes to automating boring tasks like rounding grades. Honestly, I wish math was that easy for me. I have always struggled with this and have a hard time keeping up with assignments. This is why I often need extra help and have found edubirdie.com/do-my-math-homework very helpful. It's a lifesaver when I'm stuck or just don't have enough time to finish my math homework. Does anyone find math as hard as I do?
Thanks for the feedback @beth_lewis š
Excellent article to study algorithms in JavaScript
Thank you for feedback Lala ā¤ļø
Great content Kleci!! Congrats!
Thank you my friend! š