Anyone who is familiar with the Grading students challenge on Hackerank, please help me understand why this code is not passing.
function gradingStudents(grades) {
let args = [...arguments];
const round5 = (x) => {
return Math.ceil(x / 5) * 5;
}
for (let i = 1; i < args.length; i++) {
if (round5(args[i]) - args[i] < 3 && args[i] >= 38) {
args[i] = round5(args[i])
}
}
args.shift()
return args.join('\n') + '\n'
//return args
//return args.join(' ')
}
I am losing my mind. I have tried all sorts of returns and none works.
Looks like you thought that both the number of students and the grades are being passed to the gradingStudents function, but actually only a single argument is being passed i.e. an array containing the grades of the students.
So, no need to use arguments, you can directly use the grades array. And also you simply need to return the updated grades array from the function, you don't need to worry about output formatting.
function gradingStudents(grades) {
const round5 = (x) => {
return Math.ceil(x / 5) * 5;
};
for (let i = 0; i < grades.length; i++) {
if (round5(grades[i]) - grades[i] < 3 && grades[i] >= 38) {
grades[i] = round5(grades[i]);
}
}
return grades;
}
console.log(gradingStudents([73, 67, 38, 33]));