Challenge - 5 Problems
Else-if Ladder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of else-if ladder with numeric input
What is the output of this JavaScript code?
let score = 75;
if (score >= 90) {
console.log('Grade A');
} else if (score >= 80) {
console.log('Grade B');
} else if (score >= 70) {
console.log('Grade C');
} else {
console.log('Grade F');
}Javascript
let score = 75; if (score >= 90) { console.log('Grade A'); } else if (score >= 80) { console.log('Grade B'); } else if (score >= 70) { console.log('Grade C'); } else { console.log('Grade F'); }
Attempts:
2 left
💡 Hint
Check which condition matches the score 75 first.
✗ Incorrect
The code checks conditions from top to bottom. Since 75 is not >= 90 or >= 80 but is >= 70, it prints 'Grade C'.
❓ Predict Output
intermediate2:00remaining
Output when all conditions fail in else-if ladder
What will this code print?
let temperature = 10;
if (temperature > 30) {
console.log('Hot');
} else if (temperature > 20) {
console.log('Warm');
} else if (temperature > 15) {
console.log('Mild');
} else {
console.log('Cold');
}Javascript
let temperature = 10; if (temperature > 30) { console.log('Hot'); } else if (temperature > 20) { console.log('Warm'); } else if (temperature > 15) { console.log('Mild'); } else { console.log('Cold'); }
Attempts:
2 left
💡 Hint
Check if 10 is greater than any of the conditions.
✗ Incorrect
10 is not greater than 30, 20, or 15, so the else block runs printing 'Cold'.
❓ Predict Output
advanced2:00remaining
Output of else-if ladder with string comparison
What does this code print?
let day = 'Sunday';
if (day === 'Monday') {
console.log('Start of work week');
} else if (day === 'Friday') {
console.log('End of work week');
} else if (day === 'Saturday' || day === 'Sunday') {
console.log('Weekend');
} else {
console.log('Midweek');
}Javascript
let day = 'Sunday'; if (day === 'Monday') { console.log('Start of work week'); } else if (day === 'Friday') { console.log('End of work week'); } else if (day === 'Saturday' || day === 'Sunday') { console.log('Weekend'); } else { console.log('Midweek'); }
Attempts:
2 left
💡 Hint
Check the condition that matches 'Sunday'.
✗ Incorrect
'Sunday' matches the third else-if condition, so it prints 'Weekend'.
❓ Predict Output
advanced2:00remaining
Output when else-if ladder conditions overlap
What will this code print?
let num = 5;
if (num > 0) {
console.log('Positive');
} else if (num > 3) {
console.log('Greater than three');
} else {
console.log('Other');
}Javascript
let num = 5; if (num > 0) { console.log('Positive'); } else if (num > 3) { console.log('Greater than three'); } else { console.log('Other'); }
Attempts:
2 left
💡 Hint
Remember else-if ladder stops at first true condition.
✗ Incorrect
Since num=5 is > 0, the first if block runs and prints 'Positive'. The else-if is skipped.
❓ Predict Output
expert2:00remaining
Output of else-if ladder with nested conditions and variable changes
What is the output of this code?
let x = 10;
if (x > 5) {
if (x < 15) {
x += 5;
} else if (x === 15) {
x += 10;
}
} else if (x === 10) {
x -= 5;
}
console.log(x);Javascript
let x = 10; if (x > 5) { if (x < 15) { x += 5; } else if (x === 15) { x += 10; } } else if (x === 10) { x -= 5; } console.log(x);
Attempts:
2 left
💡 Hint
Trace the nested if conditions carefully.
✗ Incorrect
x starts at 10, which is > 5 and < 15, so x += 5 runs making x = 15. The else-if inside does not run. The outer else-if is skipped.