Switch Statements
Switch Statements
π¨βπΌ We've been using
if/else if chains, but when you're checking one value
against many possible matches, a switch statement is often cleaner.π¨ Open
and:
- Create a
descriptionvariable to hold the feedback text - Write a
switchstatement that checks thegradevalue and sets the appropriate description:- 'A' β "Excellent"
- 'B' β "Good"
- 'C' β "Satisfactory"
- 'D' β "Needs Improvement"
- 'F' β "Failing"
- Any other value β "Invalid grade"
π° Use
switch with case branches and a default for all other values.Don't forget the
break statement after each case! Without it, execution
"falls through" to the next case.π¨ Once you've completed the exercise, run
node index.ts in the playground to
test your work!