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) => {
const grades = [gradeOne, gradeTwo, gradeThree]
let invalidGrade = false
grades.forEach(el => {if (el < 0 || el > 100) invalidGrade = true})
if (invalidGrade) return 'You have entered an invalid grade.'
}
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';
}
}
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?
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.
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.
Like’d to share mine. I’ve been enjoying the use of ternary operators.
// Write your function here:
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;
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
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
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