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?

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.

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

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
1 Like

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.

1 Like