# How to format switch/case?

HI, THERE!
here is my solution:
switch (true) {
case average > 0 && average < 59:
return (‘F’);
break;
case average > 60 && average < 69:
return (‘D’);
break;
case average > 70 && average < 79:
return (‘C’);
break;
case average > 80 && average < 89:
return (‘B’);
break;
case average > 90 && average < 100:
return (‘A’);
break;
default:
return ‘You have entered an invalid grade.’;
}

1 Like

Can anyone help me find what’s wrong here??

the output I’m getting is:
A
undefined

found it… i replaced ‘console.log()’ with ‘return’

1 Like

If you want to get real fancy, you can do the validation for the invalid grades the following way, making it more flexible in case you want to add more grades:

``````const finalGrade = (gradeOne, gradeTwo, gradeThree) => {
grades.forEach(el => {if (el < 0 || el > 100) invalidGrade = true})
}
``````

8 posts were split to a new topic: More universal and can work with any number of variables without changing much code

Hi guys, Im doing some task with the final grades in this topic. I tried to go with the switch syntax but somehow the results is always: undefined. Could you tell me the problem as I checked with what I learned, the syntax is written right. Thank you for your help!

You have not declared the invalid grade part as the FIRST if statement for the entire program. I checked the hints and it said to do the invalid grade statement first and did it before implementing the rest of the conditions.

Hy guys! This is my solution using switch statement.

``````const finalGrade = (a, b, c) => {
if ((a < 0 || a > 100)||(b < 0 || b > 100)||(c < 0 || c > 100)) {
return 'You have entered an invalid grade.'};
let average = (a+b+c)/3;
switch (true) {
case average < 60:
return 'F';
break;
case average < 70:
return 'D';
break;
case average < 80:
return 'C';
break;
case average < 90:
return 'B';
break;
default:
return 'A';
}
}
``````
1 Like

Hello!
You have to change average to true in switch statement and increase range to 101 in the last case.
This may help, I believe.

1 Like

Hello everyone and thanks for the inspiration.
I’ve been trying using a ternary operation before giving up, anyone as given it a try? could it be a solution?

In the meanwhile this is my code now…

``````const finalGrade = (num1, num2, num3) => {
if ((num1 < 0 || num1 > 100) || (num2 < 0 || num2 > 100) || (num3 < 0 || num3 > 100)) {
return 'You have entered an invalid grade.';
} else if (((num1 + num2+ num3)/3) < 59) {
return 'F';
} else if (((num1 + num2+ num3)/3) < 69) {
return 'D';
} else if (((num1 + num2+ num3)/3) < 79) {
return 'C';
} else if (((num1 + num2+ num3)/3) < 89) {
return 'B';
}
else {
return 'A';
}
}
``````

Hi guys! Why do we have write here (true) instead of averageGrade?

`true` is a boolean. A switch case must match the parameter expression in type and value. In the case above the expressions are comparisons (boolean) so their type matches. Whichever comparison comes away as true (a value) will be the branch that is followed.

2 Likes

1 Like

Thank you for sharing this CASE switch with us. I was trying to wrap my head around how to write the expression. I was not sure on how what expression to set. Cause there were three values I was not sure if all three needed to be set in the switch expression. Now seeing how you made the switch condition and set the expression to TRUE and just test all the cases makes sense.

Thanks

2 Likes

Like’d to share mine. I’ve been enjoying the use of ternary operators.

const finalGrade = (a, b, c) => {
if (a < 0 || b < 0 || c < 0 || a > 100 || b > 100 || c > 100){
return ‘You have entered an invalid grade.’
} else {
const avgNum = (a+b+c)/3;

``````return avgNum <= 59 ? 'F'
: avgNum <= 69 ? 'D'
: avgNum <= 79 ? 'C'
: avgNum <= 89 ? 'B'
: 'A'
``````

}
};

1 Like

Why was this code considered wrong (it passed all the checks). Ultimately, my initial if statement wasn’t accepted.

``````const finalGrade = (grade1, grade2, grade3) => {
if (
) {
return "You have entered an invalid grade.";
}
switch (true) {
return "F";
break;
return "D";
break;
return "C";
break;
return "B";
break;
return "A";
break;
}
};
``````

Why is this if statement considered incorrect? Thanks.

Thanks for your answer that helps me resolve my switch statement, but I have a question, why in the switch statement did you use (true) instead of the average variable?
I am a bit confused

12 posts were split to a new topic: Format Switch Case

Hi All, I didn’t do my else if blocks the same way as the solution code, but it worked anyway and got the ‘green check mark’ to proceed whoop #liveforgreenchecks lol

Code here:

const finalGrade = (num1, num2, num3) => {
if ((num1 < 0 || num1 > 100) || (num2 < 0 || num2 > 100) || (num3 < 0 || num3 > 100)) {
return ‘You have entered an invalid grade.’
}

let average = (num1 + num2 + num3) / 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’;
}
};

Your code works and passes because the lesson checker is not testing with floats. What happens if the grade is 59.5, 69.1, 79.3, 89.7? None of them will work and the return will be `undefined`. Can you devise a version of this function that can deal with floats?