While loop in Javascript - Time & Space Complexity
We want to understand how the time a while loop takes changes as the input grows.
How does the number of steps grow when the loop runs more times?
Analyze the time complexity of the following code snippet.
let i = 0;
while (i < n) {
console.log(i);
i++;
}
This code prints numbers from 0 up to n-1 using a while loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop runs and prints a number each time.
- How many times: It repeats exactly n times, increasing i by 1 each time.
Each time n grows, the loop runs more times, directly matching n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The number of steps grows evenly as n grows.
Time Complexity: O(n)
This means the time grows in a straight line with the input size.
[X] Wrong: "The while loop always takes the same time no matter what."
[OK] Correct: The loop runs more times when n is bigger, so it takes longer.
Understanding how loops grow with input helps you explain your code clearly and shows you know how to write efficient programs.
"What if we increased i by 2 each time instead of 1? How would the time complexity change?"
Practice
while loop do in JavaScript?Solution
Step 1: Understand the purpose of a while loop
Awhileloop runs repeatedly as long as its condition stays true.Step 2: Compare options with the definition
Only Repeats code while a condition is true correctly describes this behavior.Final Answer:
Repeats code while a condition is true -> Option AQuick Check:
while loop = repeats while true [OK]
- Thinking while loops run a fixed number of times
- Confusing while with if statement
- Assuming while loops run at least once
while loop in JavaScript?Solution
Step 1: Recall JavaScript while loop syntax
The correct syntax uses parentheses around the condition and curly braces for the code block:while (condition) { ... }.Step 2: Check each option
while (condition) { /* code */ } matches the correct syntax exactly.Final Answer:
while (condition) { /* code */ } -> Option BQuick Check:
Correct syntax = while (condition) { } [OK]
- Omitting parentheses around the condition
- Using a semicolon after while condition
- Adding a colon after while keyword
let count = 1;
while (count <= 3) {
console.log(count);
count++;
}Solution
Step 1: Analyze the loop condition and increments
The loop starts with count = 1 and runs while count is less than or equal to 3. Each time, it prints count and then adds 1.Step 2: Trace the output values
It prints 1, then 2, then 3. When count becomes 4, the condition fails and loop stops.Final Answer:
1 2 3 -> Option DQuick Check:
Prints 1 to 3 inclusive [OK]
- Including 4 in output
- Starting count at 0 instead of 1
- Printing in reverse order
let i = 0;
while i < 5 {
console.log(i);
i++;
}Solution
Step 1: Check while loop syntax
JavaScript requires parentheses around the condition in a while loop.Step 2: Identify the error in the code
The code haswhile i < 5 {but it should bewhile (i < 5) {.Final Answer:
Missing parentheses around the condition -> Option CQuick Check:
while needs (condition) [OK]
- Forgetting parentheses around condition
- Adding semicolon after while condition
- Misusing increment operator
while loop. Which code correctly does this?Solution
Step 1: Understand the goal and loop conditions
We want to print even numbers from 2 to 10 inclusive. So the loop must start at 2 and run while number is less than or equal to 10.Step 2: Check each option for correctness
let num = 2; while (num <= 10) { console.log(num); num += 2; } starts at 2, prints number, then adds 2, stopping at 10. This prints 2,4,6,8,10 correctly. let num = 1; while (num < 10) { if (num % 2 === 0) console.log(num); num++; } starts at 1, uses modulo check, but since num < 10 stops after 9, printing 2,4,6,8 missing 10. let num = 2; while (num < 10) { console.log(num); num += 2; } stops before 10, missing 10. let num = 0; while (num <= 10) { console.log(num); num += 2; } starts at 0, which is not in the required range.Final Answer:
let num = 2; while (num <= 10) { console.log(num); num += 2; } -> Option AQuick Check:
Start 2, add 2, stop at 10 [OK]
- Starting at 0 or 1 instead of 2
- Stopping before 10
- Not incrementing by 2
Ready to prove it?
Pass the test and this pattern moves unknown
