Why loops are needed in Javascript - Performance Analysis
Loops help us repeat actions many times without writing the same code again and again.
We want to know how the time to run code changes when we repeat tasks using loops.
Analyze the time complexity of the following code snippet.
const numbers = [1, 2, 3, 4, 5];
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
console.log(sum);
This code adds all numbers in an array using a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each number to sum inside the loop.
- How many times: Once for each number in the array.
As the array gets bigger, the loop runs more times, doing more additions.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the list gets longer.
[X] Wrong: "The loop runs the same time no matter how big the array is."
[OK] Correct: The loop runs once for each item, so more items mean more work and more time.
Understanding how loops affect time helps you explain how your code handles bigger data smoothly.
"What if we used two loops one inside the other to add numbers? How would the time complexity change?"
Practice
for or while loops help us to:Solution
Step 1: Understand the purpose of loops
Loops allow repeating the same action many times without rewriting the code.Step 2: Compare options with loop purpose
Only Repeat a task multiple times without writing it again matches this purpose by saying loops repeat tasks multiple times.Final Answer:
Repeat a task multiple times without writing it again -> Option AQuick Check:
Loops repeat tasks = B [OK]
- Thinking loops slow down the program
- Believing loops stop the program
- Confusing loops with one-time code
for loop in JavaScript?Solution
Step 1: Recall JavaScript for loop syntax
The correct syntax uses parentheses with initialization, condition, and increment separated by semicolons.Step 2: Match options to syntax
for (let i = 0; i < 5; i++) { } matches this exactly:for (let i = 0; i < 5; i++) { }.Final Answer:
for (let i = 0; i < 5; i++) { } -> Option AQuick Check:
JS for loop syntax = D [OK]
- Using Python or other language syntax
- Missing parentheses or semicolons
- Using wrong keywords like 'loop'
for (let i = 1; i <= 3; i++) {
console.log(i);
}Solution
Step 1: Analyze loop start and end values
The loop starts at i=1 and runs while i is less than or equal to 3.Step 2: List printed values
It prints 1, then 2, then 3, then stops because i becomes 4.Final Answer:
1 2 3 -> Option BQuick Check:
Loop prints 1 to 3 = C [OK]
- Starting count from 0 instead of 1
- Including number 4 by mistake
- Printing in reverse order
for (let i = 0; i < 5; i++) console.log(i) }
Solution
Step 1: Check braces in the code
The loop has no opening brace { but has a closing brace } at the end.Step 2: Identify mismatch
Extra closing brace } without a matching opening brace causes syntax error.Final Answer:
Extra closing brace } without matching { -> Option DQuick Check:
Brace mismatch error = A [OK]
- Forgetting to add opening brace {
- Assuming missing semicolon causes error here
- Not noticing extra closing brace
Solution
Step 1: Understand the goal
We want to print even numbers from 2 to 10 inclusive.Step 2: Check each option
for (let i = 2; i <= 10; i += 2) { console.log(i); } starts at 2, increments by 2, and stops at 10, printing 2,4,6,8,10 correctly.Final Answer:
for (let i = 2; i <= 10; i += 2) { console.log(i); } -> Option CQuick Check:
Increment by 2 from 2 to 10 = A [OK]
- Printing odd numbers by mistake
- Stopping before 10
- Using wrong increment step
Ready to prove it?
Pass the test and this pattern moves unknown
