I tried to solve the Hackerank challenge of grading students.
whereby my codes work in my local system, yet hackerank does not pass it?
Please, can someone help?
Below is the code I figured out to solve the problem.
function gradingStudents(grades) {
// Write your code here
if (grades < 38){
return grades
} else {
for (let i = 40; i <= 100; i = i + 5){
while ( i - grades < 5 && i - grades > 0){
if (i - grades < 3){
return i
} else{
return grades
}
}
}
}
}
Thank you, I overlooked that. So what do you suggest I do please, do I need to array.push or something?
any hints please
Thank you very much. Let me figure out the rest. I really appreciate your prompt assistance.
Greetings!
I found a solution to the challenge. It did pass the test.
The problem only arises when I submit the solution.
I get to score 7/12. Meaning I failed in 5 cases. I don’t know why.
Below is the code I developed. Please any ideas as to why the code won’t work in all cases?
…
…
function gradingStudents(grades) {
// Write your code here
let b =
for (let i = 0; i < grades.length; i++){
for (i of grades){
if (i < 38 ) {
b.push(i)
}else {
if ((parseInt(i) % 5) < 3){
b.push(parseInt(i))
} else{
let c = parseInt(i) + (5-(parseInt(i) % 5))
b.push(c)
}
}
}
}
return b
}
Great job. I came up with the following:
function gradingStudents(grades) {
function getNextMultipleOf5(num) {
const nextNum = num + 1;
return nextNum % 5 !== 0 ? getNextMultipleOf5(nextNum) : nextNum;
}
return grades.map(grade => {
if (grade < 38) {
return grade;
}
const nextMultipleOf5 = getNextMultipleOf5(grade);
return nextMultipleOf5 - grade < 3 ? nextMultipleOf5 : grade;
});
}
Wow! This is better. Thank you very much.