0

This is a simple grading program implementation in JavaScript. Unfortunately, it returns undefined for all the input I provide.

    const finalGrade = (homework, midterm, final) => {
      if ((midterm < 0 || midterm > 100) || (final < 0 || final > 100) || (homework < 0 || homework > 100)) {
        return "A value is out of bounds";
      }
      const average = (homework + midterm + final)/3;

      switch (average){
      case (average < 60):return 'F';break;
      case (average < 70):return 'D';break;
      case (average < 80):return 'C';break;
      case (average < 90):return 'B';break;
      case (average < 101):return 'A';break;
      default: "You have entered an invalid grade.";
      }
    };

 console.log(finalGrade(99, 92, 95)) // Should print 'A'
Isaac Attuah
  • 119
  • 1
  • 11
  • [`switch...case...`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch): _"The `switch` statement evaluates an expression, matching the expression's value to a `case` clause, and executes statements associated with that `case`, as well as statements in `case`s that follow the matching `case`."_ – Andreas May 15 '20 at 16:49

1 Answers1

1

You can't use switch ... case like that (see comments to your question). You can use a ternary to return the desired result:

const finalGrade = (homework, midterm, final) => {
      if ((midterm < 0 || midterm > 100) || 
          (final < 0 || final > 100) || 
          (homework < 0 || homework > 100)) {
        return "A value is out of bounds";
      }
      const average = (homework + midterm + final)/3;
      return average < 60 ? "D" :
             average < 80 ? "C" :
             average < 90 ? "B" :
             average < 101 ? "A" : "You have entered an invalid grade.";
    };

 console.log(finalGrade(99, 92, 95)) // Should print 'A'
 console.log(finalGrade(20, 12, 20)) // Should print 'D'
.as-console-wrapper { top: 0; max-height: 100% !important; }
KooiInc
  • 119,216
  • 31
  • 141
  • 177