 # How to format switch/case?

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

const showLetterGrade = gpa => {
/* switch (gpa){
case (gpa>0 && gpa<=59):
return "F";
break;
}*/
if (gpa>0 && gpa<=59){
return 'F';
}
}
``````

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?

1 Like

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? 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) {
break;
}
``````
`````` > grade1 = 1, grade2 = 100, grade3 = 10
> switch(true) {
break;
}
<- 37
``````
2 Likes

Thanks for that , 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.

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

2 Likes