# How to format switch/case?

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.

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.

1 Like

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

1 Like

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?

Hello everyone! Can someone please tell me how can i call at the same time both the “let average” and the function “finalGrade”. I just cant figure it out, nor return or console.log(average) have worked. I would like to see both info on the screen while as of right now, i can only print one of them at a time.

Const finalGrade = (n1, n2, n3) =>{
let average = (n1+n2+n3)/3;
if ((n1 < 0 || n1> 100) || (n2 < 0 || n2 >100) || (n3 < 0 || n3 > 100)) {
return ‘You have entered an invalid grade.’;
}
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;

}

}

`average` is a local variable so cannot be seen outside of the function.

Would suggest moving that line down to above the switch statement, after the validation.

Concerning, `break`, when return is used, that is unreachable and can be safely removed.

With regard to the complex logic, that is overkill, and has no ability to deal with floats.

Also, consider that your validation has already determined the grades to be within range, so we don’t need to test against zero (0) or 100 again.

If you wish to log the average, do it before the switch.

``````x < 60
x < 70
x < 80
x < 90
default
``````

Those are your cases (x is ‘average’). In my humble view, the simplest form makes the most sense. What’s more, it can deal with floats and does not have any ‘holes’, such as your code has between 59 and 60, between 69 and 70, between 79 and 80, and between 89 and 90.

Thanks a lot for your quick reponse, I really apreciate the explanations really useful and pedagogical! I´ll take your advices for the future. You r right my code is unnecessarily complex, but so i am. lol