Nested conditional statements in Javascript - Time & Space Complexity
We want to see how the time it takes to run nested conditional statements changes as input changes.
Specifically, we ask: does adding more conditions make the program slower as inputs grow?
Analyze the time complexity of the following code snippet.
function checkNumber(num) {
if (num > 0) {
if (num % 2 === 0) {
return 'Positive even';
} else {
return 'Positive odd';
}
} else {
return 'Non-positive';
}
}
This code checks if a number is positive and then if it is even or odd.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated steps; only simple checks.
- How many times: Each condition runs once per function call.
The time to run this code does not increase when the input number gets bigger.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 checks |
| 100 | 3 checks |
| 1000 | 3 checks |
Pattern observation: The number of steps stays the same no matter the input size.
Time Complexity: O(1)
This means the time to run the code stays constant no matter how big the input is.
[X] Wrong: "More conditions always make the program slower as input grows."
[OK] Correct: Nested conditions run only once per input, so they do not slow down with bigger inputs.
Understanding that nested conditions do not add time with bigger inputs helps you explain code efficiency clearly and confidently.
"What if we added a loop inside the nested conditions? How would the time complexity change?"
Practice
if (condition1) { if (condition2) { /* code */ } }Solution
Step 1: Understand the structure of nested conditionals
Nested conditionals mean one if statement is placed inside another if statement's block.Step 2: Identify the meaning of the code snippet
The code checks condition1 first, and only if true, it checks condition2 inside it.Final Answer:
A condition inside another condition -> Option BQuick Check:
Nested conditional = condition inside condition [OK]
- Thinking nested means conditions checked together
- Confusing nested conditionals with loops
- Assuming functions are involved
Solution
Step 1: Check proper use of braces and parentheses
if (a > 0) { if (b > 0) { console.log('Both positive'); } } uses parentheses for conditions and braces to group code blocks correctly.Step 2: Verify syntax correctness
The other options have missing or misplaced braces or parentheses causing syntax errors.Final Answer:
if (a > 0) { if (b > 0) { console.log('Both positive'); } } -> Option AQuick Check:
Correct nested if syntax = if (a > 0) { if (b > 0) { console.log('Both positive'); } } [OK]
- Missing braces for inner if block
- Omitting parentheses around conditions
- Extra or misplaced braces
let x = 5;
if (x > 0) {
if (x < 10) {
console.log('A');
} else {
console.log('B');
}
} else {
console.log('C');
}
Solution
Step 1: Evaluate the outer if condition
x = 5, which is greater than 0, so the outer if block runs.Step 2: Evaluate the inner if condition
Inside, x < 10 is true (5 < 10), so it prints 'A'.Final Answer:
A -> Option DQuick Check:
5 > 0 and 5 < 10 prints 'A' [OK]
- Ignoring inner else block
- Confusing output with 'B' or 'C'
- Assuming no output if conditions are true
let num = 10;
if (num > 5) {
if num < 15 {
console.log('Inside range');
}
}
Solution
Step 1: Check syntax of inner if statement
Inner if is written asif num < 15 {missing parentheses around condition.Step 2: Confirm correct syntax for if statements
JavaScript requires parentheses around conditions:if (num < 15) {.Final Answer:
Missing parentheses around inner if condition -> Option CQuick Check:
if conditions need parentheses [OK]
- Forgetting parentheses in if conditions
- Assuming braces are optional
- Thinking console.log causes error
- 'Small positive' if a number is > 0 and < 10
- 'Large positive' if a number is >= 10
- 'Non-positive' otherwise
Which code correctly implements this?
Solution
Step 1: Check conditions for small positive numbers
if (num > 0) { if (num < 10) { console.log('Small positive'); } else { console.log('Large positive'); } } else { console.log('Non-positive'); } checks if num > 0, then inside if num < 10 to print 'Small positive'.Step 2: Check conditions for large positive and non-positive
Else inside outer if prints 'Large positive' for num >= 10, else prints 'Non-positive'.Step 3: Verify other options
The option with combined conditions (&& and else if) is logically correct but does not use nested if statements. The option with outer condition num >= 0 incorrectly classifies 0 as 'Small positive'. The option with inner condition num <= 10 incorrectly classifies 10 as 'Small positive'.Final Answer:
if (num > 0) { if (num < 10) { console.log('Small positive'); } else { console.log('Large positive'); } } else { console.log('Non-positive'); } -> Option AQuick Check:
Nested if with correct ranges = if (num > 0) { if (num < 10) { console.log('Small positive'); } else { console.log('Large positive'); } } else { console.log('Non-positive'); } [OK]
- Using <= instead of < for small positive
- Including zero as positive
- Not nesting conditions properly
Ready to prove it?
Pass the test and this pattern moves unknown
