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
index.ts
. The starter sets grade to 'B'.
  1. Create a description string
  2. Write a switch on grade that sets description to these exact values:
    • 'A' β†’ "Excellent"
    • 'B' β†’ "Good"
    • 'C' β†’ "Satisfactory"
    • 'D' β†’ "Needs Improvement"
    • 'F' β†’ "Failing"
    • any other value β†’ "Invalid grade"
  3. Export grade and description

Required exports

grade, description

Completion criteria (with grade = 'B')

  • grade remains "B"
  • description is "Good"
πŸ’° 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!

Please set the playground first

Loading "Switch Statements"
Loading "Switch Statements"