Why conditional logic is needed in Javascript - Performance Analysis
We want to see how adding choices in code affects how long it takes to run.
How does the program's work change when it decides between options?
Analyze the time complexity of the following code snippet.
function checkNumber(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] > 10) {
return true;
}
}
return false;
}
This code looks through an array and stops as soon as it finds a number bigger than 10.
- Primary operation: Looping through the array elements one by one.
- How many times: Up to the length of the array, but may stop early if condition is met.
As the array gets bigger, the number of checks can grow, but sometimes it stops early.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Between 1 and 10 checks |
| 100 | Between 1 and 100 checks |
| 1000 | Between 1 and 1000 checks |
Pattern observation: The work can be less if the condition is met early, but in the worst case it grows with the array size.
Time Complexity: O(n)
This means the time to finish grows roughly in step with the number of items to check.
[X] Wrong: "Because the loop can stop early, the time is always small and constant."
[OK] Correct: Sometimes the condition is never met, so the loop checks every item, making time grow with input size.
Understanding how choices in code affect speed helps you explain your thinking clearly and write efficient programs.
"What if we changed the condition to check every item without stopping early? How would the time complexity change?"
Practice
if and else in JavaScript?Solution
Step 1: Understand the purpose of conditional logic
Conditional logic allows the program to choose between different actions depending on conditions.Step 2: Compare options with this purpose
Only To make decisions and run different code based on conditions describes making decisions based on conditions, which matches the purpose ofifandelse.Final Answer:
To make decisions and run different code based on conditions -> Option AQuick Check:
Conditional logic = decision making [OK]
- Confusing conditional logic with loops
- Thinking it stores data
- Believing it only creates variables
age is greater than 18 in JavaScript?Solution
Step 1: Recall JavaScript
JavaScript requires parentheses around the condition and curly braces for the code block:ifstatement syntaxif (condition) { }.Step 2: Check each option
if (age > 18) { } matches the correct syntax. Options A and C miss parentheses or use invalid keywords like 'then'. if (age > 18) then { } incorrectly uses 'then'.Final Answer:
if (age > 18) { } -> Option AQuick Check:
Correctifsyntax = parentheses + braces [OK]
- Omitting parentheses around condition
- Using 'then' keyword (not in JavaScript)
- Missing curly braces
let score = 75;
if (score >= 60) {
console.log('Pass');
} else {
console.log('Fail');
}Solution
Step 1: Check the condition
The variablescore >= 60scoreis 75, which is greater than or equal to 60, so the condition is true.Step 2: Determine which block runs
Since the condition is true, the code inside theifblock runs, printing 'Pass'.Final Answer:
Pass -> Option DQuick Check:
75 >= 60 is true, so output = Pass [OK]
- Confusing which block runs on true condition
- Thinking output is 'Fail' for true condition
- Assuming syntax error
let temperature = 30;
if temperature > 25 {
console.log('Hot');
} else {
console.log('Cold');
}Solution
Step 1: Check the
JavaScript requires parentheses around the condition in anifstatement syntaxifstatement, but here they are missing.Step 2: Verify other parts
The variable declaration is correct with semicolon,elseis correctly used, and variable name matches.Final Answer:
Missing parentheses around the condition in if statement -> Option CQuick Check:
ifneeds (condition) [OK]
- Forgetting parentheses in if condition
- Thinking semicolon is mandatory after if
- Confusing else usage
Solution
Step 1: Understand the conditions for each greeting
'Good morning' if hour < 12, 'Good afternoon' if hour between 12 and 18 inclusive, else 'Good evening'.Step 2: Check each option's condition ranges
if (hour < 12) { console.log('Good morning'); } else if (hour <= 18) { console.log('Good afternoon'); } else { console.log('Good evening'); } correctly useshour < 12for morning,hour <= 18for afternoon (including 12 and 18), else evening. Others miss inclusive 12 or 18 or have wrong ranges.Final Answer:
if (hour < 12) { console.log('Good morning'); } else if (hour <= 18) { console.log('Good afternoon'); } else { console.log('Good evening'); } -> Option BQuick Check:
Inclusive afternoon means <= 18, so if (hour < 12) { console.log('Good morning'); } else if (hour <= 18) { console.log('Good afternoon'); } else { console.log('Good evening'); } fits [OK]
- Using < instead of <= for inclusive ranges
- Overlapping or missing ranges
- Wrong logical operators in conditions
Ready to prove it?
Pass the test and this pattern moves unknown
