DEV Community

loading...

Grading Students

Muhammad Uzair
ReactJs | React-Native | JavaScript | Nodejs | MongoDB | UserInterface | Python
・1 min read

while working with hacker-rank problem-solving challenge, I just stuck in question of finding the results according to given criteria.
Criteria:
1) Find the next number from given input that is divisible by 5.

    i-e => Input is  73 so the number will be 75
Enter fullscreen mode Exit fullscreen mode

2) Find the difference between actual input and the discovered.

    i-e => 73 -75 
Enter fullscreen mode Exit fullscreen mode

3) If the difference between them is smaller than 3 then the
student will be awarded a discovered number as a grade else it
will remain the same as input

  i-e 
    1) Input 73 - 75 = 2 //difference is less than 3 So, Grade 
    will be 75 here
    2) Input  67 - 70  = 3 // difference is less than 3 So, 
 Grade will be 67 here
Enter fullscreen mode Exit fullscreen mode

4) If input is 33 or less then simply return it no need for any
processing

Solution:

  function gradingStudents(grades) {
   let final = [];
     for (let b = 0; b < grades.length; b++) {
      let base = parseInt(grades[b]);
       let val = parseInt(grades[b]);
       if (base < 38) {
        final.push(base);
        } else {
        for (let a = 0; a < 5; a++) {
            if (val % 5 == 0) {
                if (val - base < 3) {
                    final.push(val);
                    break;
                } else {
                    final.push(base);
                    break;
                }
            } else {
                val++;
            }
        }
    }
}
return final
 }
Enter fullscreen mode Exit fullscreen mode

Hope you find this helpful or if there is any optimize method of solving this kindly recommend in a comment section.
Thanks.

Discussion (0)

Forem Open with the Forem app