Challenge - 5 Problems
Switch Mastery
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 this JavaScript code?
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');
}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, switch cases fall through to the next.
✗ Incorrect
Since there is no break after case 2, it runs case 2 and case 3's console.logs, then stops at break.
❓ Predict Output
intermediate2:00remaining
Equivalent if-else output
What is the output of this if-else code?
const value = 2;
if (value === 1) {
console.log('One');
} else if (value === 2) {
console.log('Two');
} else if (value === 3) {
console.log('Three');
} else {
console.log('Default');
}Javascript
const value = 2; if (value === 1) { console.log('One'); } else if (value === 2) { console.log('Two'); } else if (value === 3) { console.log('Three'); } else { console.log('Default'); }
Attempts:
2 left
💡 Hint
Only one block runs in if-else chains.
✗ Incorrect
The value matches the second condition, so only 'Two' is printed.
❓ Predict Output
advanced2:00remaining
Switch with multiple cases combined
What will this code print?
const fruit = 'apple';
switch(fruit) {
case 'banana':
case 'apple':
console.log('Yellow or Red fruit');
break;
case 'orange':
console.log('Orange fruit');
break;
default:
console.log('Unknown fruit');
}Javascript
const fruit = 'apple'; switch(fruit) { case 'banana': case 'apple': console.log('Yellow or Red fruit'); break; case 'orange': console.log('Orange fruit'); break; default: console.log('Unknown fruit'); }
Attempts:
2 left
💡 Hint
Cases stacked without break share the same code block.
✗ Incorrect
Both 'banana' and 'apple' cases run the same console.log because they share the block.
❓ Predict Output
advanced2:00remaining
If vs switch with type coercion
What is the output of this code?
const val = '2';
switch(val) {
case 2:
console.log('Number 2');
break;
case '2':
console.log('String 2');
break;
default:
console.log('Default');
}Javascript
const val = '2'; switch(val) { 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 (===) for matching.
✗ Incorrect
The value is a string '2', so only case '2' matches exactly.
🧠 Conceptual
expert2:00remaining
Why prefer switch over if in some cases?
Which of the following is the best reason to use a switch statement instead of multiple if-else statements in JavaScript?
Attempts:
2 left
💡 Hint
Think about how code looks when checking many values of one variable.
✗ Incorrect
Switch is clearer and easier to read when testing one variable against many fixed values.