Loop execution flow in Javascript - Time & Space Complexity
When we run a loop in code, it repeats some steps many times. Understanding how this repetition grows helps us know how long the code might take as the input gets bigger.
We want to find out how the number of steps changes when the loop runs more times.
Analyze the time complexity of the following code snippet.
function printNumbers(n) {
for (let i = 0; i < n; i++) {
console.log(i);
}
}
This code prints numbers from 0 up to n-1, running the loop n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop runs and prints a number each time.
- How many times: Exactly n times, where n is the input number.
As n grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: If you double n, the work doubles too. The growth is steady and direct.
Time Complexity: O(n)
This means the time it takes grows directly with the size of the input n.
[X] Wrong: "The loop runs faster because it just prints simple numbers."
[OK] Correct: Even simple steps inside a loop add up when repeated many times. The number of repetitions matters more than what happens inside.
Understanding how loops grow with input size is a key skill. It helps you explain how your code behaves and shows you can think about efficiency clearly.
"What if we added a nested loop inside the first loop? How would the time complexity change?"
Practice
for loop do in JavaScript?Solution
Step 1: Understand the purpose of a for loop
Aforloop is designed to repeat code multiple times based on a condition.Step 2: Identify the correct description
Among the options, only repeating code a set number of times matches theforloop behavior.Final Answer:
Repeats code a set number of times -> Option DQuick Check:
for loop = repeat fixed times [OK]
- Thinking for loops run only once
- Confusing for loops with if statements
- Believing for loops stop the program
for loop in JavaScript?Solution
Step 1: Recall JavaScript for loop syntax
The correct syntax usesfor (initialization; condition; increment) { }.Step 2: Compare options to syntax
Onlyfor (let i = 0; i < 5; i++) { }matches the correct syntax withlet i = 0; i < 5; i++.Final Answer:
for (let i = 0; i < 5; i++) { } -> Option BQuick Check:
for loop syntax = initialization; condition; increment [OK]
- Using incorrect keywords like 'loop'
- Swapping initialization and condition
- Missing semicolons inside for parentheses
let sum = 0;
for (let i = 1; i <= 3; i++) {
sum += i;
}
console.log(sum);Solution
Step 1: Trace the loop iterations
The loop runs with i = 1, 2, 3. Each time, sum adds i: 0+1=1, 1+2=3, 3+3=6.Step 2: Determine final sum value
After the loop, sum is 6, which is printed by console.log.Final Answer:
6 -> Option AQuick Check:
Sum 1+2+3 = 6 [OK]
- Stopping loop early
- Adding wrong values
- Confusing sum initialization
for (let i = 0; i < 5; i--) {
console.log(i);
}Solution
Step 1: Analyze the loop increment/decrement
The loop usesi--, so i decreases each time starting from 0.Step 2: Check loop condition
The condition isi < 5, which is true for all i less than 5, including negative numbers. Since i decreases, it never reaches 5 or more, so loop never ends.Final Answer:
The loop will run infinitely because i decreases -> Option CQuick Check:
Decrement with < condition causes infinite loop [OK]
- Confusing i-- with i++
- Ignoring loop condition logic
- Assuming loop stops automatically
let count = 0;
let i = 10;
while (i > 0) {
i -= 3;
count++;
}
console.log(count);Solution
Step 1: Trace the while loop iterations
Start i=10, count=0. Each loop: i decreases by 3, count increases by 1.
Iterations:
1) i=7, count=1
2) i=4, count=2
3) i=1, count=3
4) i=-2, count=4 (loop stops after this because i > 0 is false)Step 2: Count total iterations
The loop runs 4 times before i becomes -2 and condition fails.Final Answer:
4 -> Option AQuick Check:
Subtract 3 until i ≤ 0 counts 4 times [OK]
- Counting one less or more iteration
- Not updating count correctly
- Misunderstanding while loop condition
Ready to prove it?
Pass the test and this pattern moves unknown
