Grading Students Challenge on Hackerrank

Hi Everyone,

I have a solution to the challenge on Hackerrank - Grading Students | HackerRank I was able to figure out a solution and I thought it would be cool to save it here and also get to improve on it as I learn more in Javascript

function gradingStudents1(grades){ 
  let finalGrades = grades.map((grade) => { 
      return grade >= 38 && grade % 5 >= 3  ? grade - (grade % 5) + 5 : grade;         
    }); 
  return finalGrades; 
 }

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Hi @bobb-rob !
Welcome to the forum!

Whenever asking for feedback on the forum please include the link to the challenge you are working on.
That way people will have all of the information necessary to provide feedback.

Hope that helps!

3 Likes

Thank you very much. I appreciate the help

Your IF structure could be better

What is the purpose of the grades.length? You aren’t stopping the function from returning a value, you only made it so it returns the same array if the length of array is greater than 60. Basically that if statement has no need to be inside the map (if grades is ever greater than 60, it’s going to be the case for every element you map.

I don’t like the way of reassigning the parameter as the variable name the way you did it.
It makes it confusing/hard to understand. Use different variable names

Here is my solution with TypeScript. I used a ternary to determine whether or not to add the rounding.

function gradingStudents(grades: number[]): number[] {
    // Write your code here
let roundedGrades:number[] = grades.map((item):number=>{
let addedNumber:number = 5 - (item % 5);
return item > 37 && addedNumber < 3 ? item + addedNumber : item;
})
return roundedGrades;
}

Thank you for your very useful contribution. I forgot that I could use ternary to make it less verbose. Thanks for that.

And for the if statement(if grades is ever greater than 60) inside the map function, I was trying to put a condition so the function does nothing if the grades array’s length is greater than 60. I think that was part of the constraint in the challenge.

Great solution you came up with. But, if we are to take efficiency into consideration, your space complexity becomes O(n). So, i came up with a solution with O(n) time complexity and O(1) space complexity.

function gradingStudents(grades) {
    // Write your code here   
    for (let i = 0; i < grades.length; i++) {        
        if (((grades[i] + 2) % 5 == 0) && (grades[i] + 2 > 39)) {
            grades[i] = grades[i] + 2;
        } else if (((grades[i] + 1) % 5 == 0) && (grades[i] + 2 > 39)) {
            grades[i] = grades[i] + 1;
        }
    }
    return grades;
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.