Challenge - 5 Problems
Switch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of switch with fall-through
What is the output of the following code?
Javascript
const value = 2; switch(value) { case 1: console.log('One'); case 2: console.log('Two'); case 3: console.log('Three'); break; default: console.log('Default'); }
Attempts:
2 left
💡 Hint
Remember that without break, cases fall through to the next.
✗ Incorrect
The switch starts at case 2 and prints 'Two'. Because there is no break, it continues to case 3 and prints 'Three'. Then it hits break and stops.
❓ Predict Output
intermediate2:00remaining
Switch with string cases
What will this code print?
Javascript
const fruit = 'apple'; switch(fruit) { case 'banana': console.log('Banana'); break; case 'apple': console.log('Apple'); break; case 'orange': console.log('Orange'); break; default: console.log('Unknown fruit'); }
Attempts:
2 left
💡 Hint
Check which case matches the string exactly.
✗ Incorrect
The variable fruit is 'apple', so the case 'apple' matches and prints 'Apple'.
❓ Predict Output
advanced2:00remaining
Switch with multiple cases and default
What is the output of this code snippet?
Javascript
const num = 5; switch(num) { case 1: case 3: case 5: console.log('Odd'); break; case 2: case 4: console.log('Even'); break; default: console.log('Unknown'); }
Attempts:
2 left
💡 Hint
Multiple cases can share the same code block.
✗ Incorrect
Number 5 matches case 5, which shares the block printing 'Odd'.
❓ Predict Output
advanced2:00remaining
Switch with expression and type coercion
What will this code print?
Javascript
const val = '2'; switch(val) { case 1: console.log('Number 1'); break; case 2: console.log('Number 2'); break; case '2': console.log('String 2'); break; default: console.log('Default'); }
Attempts:
2 left
💡 Hint
Switch uses strict equality (===) to compare values.
✗ Incorrect
The variable val is the string '2'. It matches case '2' exactly, so it prints 'String 2'.
🧠 Conceptual
expert2:00remaining
Behavior of switch with no matching case and no default
What happens when a switch statement has no matching case and no default case?
Attempts:
2 left
💡 Hint
Think about what happens if no case matches and no default is present.
✗ Incorrect
If no case matches and there is no default, the switch block does nothing and continues after it.