Challenge - 5 Problems
If Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested if statements
What is the output of the following code?
Javascript
let x = 10; if (x > 5) { if (x < 15) { console.log('A'); } else { console.log('B'); } } else { console.log('C'); }
Attempts:
2 left
💡 Hint
Check the conditions step by step.
✗ Incorrect
x is 10, which is greater than 5 and less than 15, so the first if and nested if both run, printing 'A'.
❓ Predict Output
intermediate2:00remaining
If statement with else if
What will be logged to the console?
Javascript
let score = 75; if (score >= 90) { console.log('Excellent'); } else if (score >= 70) { console.log('Good'); } else { console.log('Try again'); }
Attempts:
2 left
💡 Hint
Check which condition matches the score first.
✗ Incorrect
Score is 75, which is not >= 90 but is >= 70, so 'Good' is printed.
❓ Predict Output
advanced2:00remaining
If statement with logical operators
What is the output of this code?
Javascript
let a = 5; let b = 10; if (a > 3 && b < 15) { console.log('X'); } else if (a > 3 || b > 15) { console.log('Y'); } else { console.log('Z'); }
Attempts:
2 left
💡 Hint
Both conditions in the first if must be true for 'X'.
✗ Incorrect
a is 5 > 3 and b is 10 < 15, so first if condition is true, printing 'X'.
❓ Predict Output
advanced2:00remaining
If statement with type coercion
What will be the output of this code?
Javascript
let val = '0'; if (val) { console.log('True'); } else { console.log('False'); }
Attempts:
2 left
💡 Hint
Check how JavaScript treats non-empty strings in conditions.
✗ Incorrect
Non-empty strings are truthy in JavaScript, so 'True' is printed.
❓ Predict Output
expert2:00remaining
If statement with variable scope
What is the value of x after running this code?
Javascript
let x = 1; if (true) { let x = 2; if (true) { x = 3; } } console.log(x);
Attempts:
2 left
💡 Hint
Consider the scope of variables declared with let inside blocks.
✗ Incorrect
The inner x variables are block scoped and do not affect the outer x, so x remains 1.