How to format switch/case?

finalGrade()

when I try to run the code below for switch, it fails (goes for the undefined) however when I try the same logic for if when comparing if the number is in-between two other numbers, it works. Is there logic behind this? otherwise I understand that only way to understand if a number is in-between 2 other numbers is if/else if statement but not switch. I prefer switch because it’s easier to read and read since everything is under one curly braces.

const finalGrade = (n1,n2,n3)=>{
  let average = (n1+n2+n3)/3
  console.log(average);
  return showLetterGrade(average);
}

const showLetterGrade = gpa => {
 /* switch (gpa){
    case (gpa>0 && gpa<=59):
      return "F";
      break;
  }*/
  if (gpa>0 && gpa<=59){
    return 'F';
  }
}
console.log(finalGrade(40, 42, 45))

The logic is simple… When the switch parameter matches the case, that branch is followed.

switch(boolean):
case boolean:

switch(number)
case number:

switch(string)
case string:

gpa is a value (a number). Are there any cases that match this?

This is a contradiction of terms. You do understand what GPA means?

3 Likes

A switch normally only has one boolean check per case statement.

This person had the same type of problem.

The above link has 2 different ways to solve this.

You need to check the boolean of the case vs a boolean of the switch(expr).
Just change it to switch(true) then run… also make sure the default sentence has a (.) otherwise it will still show as not solved.

is there a way that you can make that you can add (as a argument) as many grades as you want? and if yes, then how do you make that the average should automatically divide according to how many grades you’ve entered?

arguments and arguments.length.

2 Likes

oh, so you make /arguments.length! but how do you add? i dont understand what i am suppose to do with the arguments

Sum of an array? Nothing special there.
I generally prefer to represent things in data rather than code - so I’d use a single argument, an array.

Is there a simpler way to check if multiple variables are within a certain numerical range? image

1 Like

There are other ways, but given the number of variables and conditions it’s doubtful any of them will be simpler. Take this bit of silliness, for example:

 > grade1 = -1, grade2 = 101, grade3 = -10
 > switch(true) {
     case 0 > grade1:
     case 0 > grade2:
     case 0 > grade3:
     case grade1 > 100:
     case grade2 > 100:
     case grade3 > 100:
       console.log('Invalid grade');
       break;
     default: console.log((grade1 + grade2 + grade3) / 3);
   }
<- Invalid grade
 > grade1 = 1, grade2 = 100, grade3 = 10
 > switch(true) {
     case 0 > grade1:
     case 0 > grade2:
     case 0 > grade3:
     case grade1 > 100:
     case grade2 > 100:
     case grade3 > 100:
       console.log('Invalid grade');
       break;
     default: console.log((grade1 + grade2 + grade3) / 3);
   }
<- 37
5 Likes

Thanks for that :+1:, I was trying with a switch statement before but then I realised that I was comparing a number (grade) to a conditional (0 > grade1) so it wouldn’t work.

2 Likes

I tried using a switch statement in my code but for some reason, I get a prompt that I did something wrong. I used different arguments to check if the switch statement works and it did including for values below 0 and above 100. In that case, it printed to the console “You have entered an invalid grade.”
Can anybody explain what is wrong with the code and why I get that prompt message? It doesn’t let me go to the next lesson either.

2 Likes

I believe you missed the initial part, to check if grade is invalid in case grade<0 or grade>100:

const finalGrade = (midterm, final, homework) => {
if ((midterm < 0 || midterm > 100) || (final < 0 || final > 100) || (homework < 0 || homework > 100)) {
return ‘You have entered an invalid grade.’;
}

6 Likes

same thing…code works, but program doesn’t accept it as correct
can not understand what the problem is

I found it, it was a “dot” at the end of ‘You have entered an invalid grade.’
So be careful :joy:

1 Like

strings returned need to be an exact match, seems you forgot the full stop at the end of your sentence.

Just copy paste the string from the instructions.

1 Like

yep, thanks! I found it

Another thing to watch out for is if you’ve copied and pasted the text you can sometimes end up with curly quotation marks instead of straight line quotations which will throw an error.

Always type your own quotations to make sure. Took me ages to see why I kept getting errors and it was down to that.

1 Like

Mine was very similar to yours and combines a switch with standard conditionals…

const finalGrade = ( g1, g2, g3 ) => {
  let outcome = String;

  switch(true) {
     case 0 > g1:
     case 0 > g2:
     case 0 > g3:
     case g1 > 100:
     case g2 > 100:
     case g3 > 100:
       outcome = undefined;
       break;
     default: outcome; break;
   }

  if (outcome === undefined){
    return 'You have entered an invalid grade.';
  } else {
    let average = ( ( g1 + g2 + g3 ) / 3 );

    if (average >= 0 && average <= 59){
      return 'F';
    } else
    if (average >= 60 && average <= 69){
      return 'D';
    } else
    if (average >= 70 && average <= 79){
      return 'C';
    } else
    if (average >= 80 && average <= 89){
      return 'B';
    } else
    if (average >= 90 && average <= 100){
      return 'A';
    }
  }
}
2 Likes

Your switch expression (gpa), is of type Integer, and your cases are of type Boolean. They must be the same.

In your default statement within your switch you are validating the average - the instructions want you to validate each grade to ensure it’s above 0 and below 100. So before you even get into your switch statement you want to check each (midterm, final, homework) if any grade is below 0 or above 100 return “You have entered an invalid grade”, else (your switch statement)