Else–if ladder in Javascript - Time & Space Complexity
We want to understand how the time taken by an else-if ladder changes as we add more conditions.
How does checking multiple conditions one after another affect performance?
Analyze the time complexity of the following code snippet.
function checkNumber(num) {
if (num === 1) {
return 'One';
} else if (num === 2) {
return 'Two';
} else if (num === 3) {
return 'Three';
} else {
return 'Other';
}
}
This code checks a number against several conditions one by one until it finds a match.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking conditions one after another in the else-if ladder.
- How many times: Up to the number of conditions, until one matches or all are checked.
As the number of conditions grows, the checks increase linearly if the match is near the end or missing.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | Up to 3 checks |
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
Pattern observation: The number of checks grows directly with the number of conditions.
Time Complexity: O(n)
This means the time to find a match grows in a straight line as you add more conditions.
[X] Wrong: "Else-if ladders always run in constant time because they stop once a match is found."
[OK] Correct: If the match is near the end or missing, many conditions are checked, so time grows with the number of conditions.
Understanding how else-if ladders scale helps you write efficient condition checks and shows you can think about performance in everyday code.
"What if we replaced the else-if ladder with a switch statement? How would the time complexity change?"
Practice
else if ladder do in JavaScript?Solution
Step 1: Understand else if ladder behavior
An else if ladder checks conditions in order, one after another.Step 2: Identify which block runs
Only the first condition that is true will run its code block; others are skipped.Final Answer:
Checks multiple conditions one by one and runs the first true block -> Option CQuick Check:
else if ladder = first true block runs [OK]
- Thinking all true blocks run
- Confusing else if with separate if statements
- Believing else if runs last condition only
Solution
Step 1: Check JavaScript syntax rules
JavaScript uses parentheses around conditions and keywords: if, else if, else.Step 2: Identify correct else if syntax
The correct keyword iselse ifwith a space, notelseifor missing parentheses.Final Answer:
if (x > 10) {} else if (x > 5) {} else {} -> Option AQuick Check:
Correct else if syntax = if (x > 10) {} else if (x > 5) {} else {} [OK]
- Writing 'elseif' as one word
- Omitting parentheses around conditions
- Using else with a condition directly
let score = 75;
if (score >= 90) {
console.log('A');
} else if (score >= 80) {
console.log('B');
} else if (score >= 70) {
console.log('C');
} else {
console.log('F');
}Solution
Step 1: Check conditions in order
score is 75, so first condition (>=90) is false, second (>=80) is false, third (>=70) is true.Step 2: Identify which block runs
The third else if block runs and prints 'C'. The else block is skipped.Final Answer:
C -> Option BQuick Check:
First true condition output = C [OK]
- Picking output 'B' or 'A' incorrectly
- Ignoring order of conditions
- Assuming else runs when a condition is true
let num = 10;
if (num > 10) {
console.log('Greater than 10');
} else if num > 5 {
console.log('Greater than 5');
} else {
console.log('5 or less');
}Solution
Step 1: Check syntax of else if condition
In JavaScript, conditions must be inside parentheses, but here else if condition lacks parentheses.Step 2: Confirm other parts are correct
Curly braces are present, and else if is correctly used, so no other syntax errors.Final Answer:
Missing parentheses around condition in else if -> Option DQuick Check:
Conditions need parentheses [OK]
- Forgetting parentheses in else if
- Confusing else if with else
- Missing curly braces
let score = 45;
if (score < 0) {
console.log('Invalid');
} else if (score >= 50) {
console.log('Pass');
} else {
console.log('Fail');
}Solution
Step 1: Check order of conditions
Negative score is checked first, so invalid scores are caught early.Step 2: Verify pass and fail conditions
Score >= 50 prints 'Pass', else prints 'Fail'. This matches requirements.Final Answer:
Correctly assigns grades as per conditions -> Option AQuick Check:
Order and conditions correct = Correctly assigns grades as per conditions [OK]
- Checking pass before invalid score
- Swapping pass and fail conditions
- Missing else block for fail
Ready to prove it?
Pass the test and this pattern moves unknown
