We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

functiongradingStudents(grades){// Write your code hereletfinalGrades=newArray(grades.length);for(letx=0;x<=grades.length-1;x++){if(grades[x]>=38&&(grades[x]+2)%5===0){finalGrades[x]=grades[x]+2;}elseif(grades[x]===99||(grades[x]>=38&&(grades[x]+1)%5===0)){finalGrades[x]=grades[x]+1;}else{finalGrades[x]=grades[x];}}returnfinalGrades;}

## Grading Students

You are viewing a single comment's thread. Return to all comments →

Javascript with ES6 :)

Wow that's short!

I think you can make it even shorter without hurting readability too badly.

This would work too and is probably a little clearer.

I did the same, and was making the same decision between Math.ceil(g/5)5 and g+(5-(g%5)) I ended up with the following:

`const gradingStudents = grades => grades.map(grade=> grade <= 37 || grade % 5 < 3 ? grade : grade + (5 - grade % 5));`

Short and sweet unlike mine lol

Totally forgot about the map method too.

My JS solution:

Can someone explain why he substracts the

`(n % 5)`

from`5`

in line 3?n%5 will give you the remainder of the numbers to make n divisible by 5..

say n = 78

78%5 = 3 // so we have a remainder of 3 if we divide 78/5..

We however do not need the remainder to convert to a number divisible by 5 say we add 3 to 78 we will end up with 81, which is not divisible by 5.

We want to know how far we are from achieving a 5 from the modulus of 78 which is why we then subtract the modulus from 5..

In our case it would be we have a remainder of 3 and from 78 we require 2 (i.e 5-3(remainder) ) to be able to make our number divisible by 5..

sense?

thanks