educative.io

Test failed even though my code is right and it works fine on VS Code

Because of the way Educative tests multiple inputs together, I keep getting test fails even though my code is right and it works fine. Is there a way to test only for a single input instead of multiple?

Here’s an example:

function gpaPoint($grade) {
switch ($grade) {
case ‘A+’:
echo 4;
break;
case ‘A’:
echo 4;
break;
case ‘A-’:
echo 3.7;
break;
case ‘B+’:
echo 3.3;
break;
case ‘B’:
echo 3;
break;
case ‘B-’:
echo 2.8;
break;
case ‘C+’:
echo 2.5;
break;
case ‘C’:
echo 2;
break;
case ‘C-’:
echo 1.8;
break;
case ‘D’:
echo 1.5;
break;
case ‘F’:
echo 0;
break;
default:
echo ‘Incorrect grade entered, check your input.’;
break;
}
}

This code works fine when I test for a single input by calling the function like:
gpaPoint(‘B+’);

But when Educative tests it for multiple inputs at once, we get an output like:
3.72.5430 (the tests were run simultaneously for gpaPoint(A-), gpaPoint(C+), gpaPoint(A), gpaPoint(B), gpaPoint(F) so the answers are 3.7, 2.5, 4, 3, 0 respectively.

As a result Educative is showing that my test failed even though my code was correct. Is there a way to fix this as it is annoying?

The syntax of the code is correct, but the only thing you need to do is to write a return statement instead of only printing the answer through the echo command. The following code would work:

function gpaPoint($grade){
  switch ($grade) {
  case "A+":
  return 4;
  break;
  case "A":
  return 4;
  break;
  case "A-": 
  return 3.7;
  break;
  case "B+":
  return 3.3;
  break;
  case "B":
  return 3;
  break;
  case "B-":
  return 2.8;
  break;
  case "C+":
  return 2.5;
  break;
  case "C":
  return 2;
  break;
  case "C-":
  return 1.8;
  break;
  case "D":
  return 1.5;
  break;
  case "F":
  return 0;
  break;
  default:
  return "Incorrect grade entered, check your input.";
  break;
  }
}