Why do I get an undefined return for my grade program?

Isaac Attuah

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'
KooiInc

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; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why do I get no output for my program

Why do I get the same readout on my ASCII to number program?

Why do I get deadlock in my go program?

Why do I get a NullPointerException for my SmarterSorter program?

Why do I get "Undefined variable" in my custom PHP function

Why do i get extra undefined in last row of my output?

Why do I get 'this is undefined' in my Octane component methods?

Why do I get "undefined local variable or method" in my code?

How do I get the average age or grade of my stored list

Why does my function return undefined whilst I do give it an argument

Why is my program only printing one grade?

Why does my id i get from next js router.query return undefined on refresh?

How do I get my javascript code to calculate the numbers and not return undefined or Nan?

Why do I get an error (Notice: Undefined index) when I try to echo a value from my database?

Why do I get a missing return statement error although there is a return statement in each of my if and if else blocks?

Why do I get a 'constructor is undefined` error?

Why do I get property 'map' is undefined

why do i get undefined in the console in JavaScript

Why do I get undefined plus the value?

Why do I get property of undefined error?

Why do I get undefined here?

Why do I get undefined variable in an if statement?

Why do i get QJsonValue(undefined)?

How do I fix an undefined variable in my program?

Why do I get an error saying that the program can't find my text file

Why do I get a segmentation error in my Stream Editor program for larger input sizes?

Why do I get popping noises from my Core Audio program?

Why do I get an TypeError: 'in <string>' requires string as left operand, not list error when running my program?

Why do does my jquery variable return undefined in firebug console