Function execution flow in Javascript - Time & Space Complexity
We want to see how the time a function takes changes as we give it bigger inputs.
How does the function's work grow when the input grows?
Analyze the time complexity of the following code snippet.
function sumArray(arr) {
let total = 0;
for (let i = 0; i < arr.length; i++) {
total += arr[i];
}
return total;
}
This function adds up all numbers in an array and returns the total.
- Primary operation: The for-loop that goes through each item in the array.
- How many times: Once for every element in the array.
As the array gets bigger, the function does more additions, one for each item.
| 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 with the input size.
[X] Wrong: "The function takes the same time no matter how big the array is."
[OK] Correct: Because the function must add each number, more numbers mean more work.
Understanding how function time grows helps you explain your code clearly and shows you know how to write efficient programs.
"What if we changed the function to sum only the first half of the array? How would the time complexity change?"
Practice
Solution
Step 1: Understand function definition
Defining a function only tells the program what the function does but does not run it.Step 2: Recognize function call requirement
The function code runs only when the function is called explicitly.Final Answer:
The function code does not run. -> Option BQuick Check:
Function runs only when called [OK]
- Thinking function runs on definition
- Confusing declaration with execution
- Assuming automatic call
greet in JavaScript?Solution
Step 1: Identify function call syntax
In JavaScript, calling a function requires its name followed by parentheses.Step 2: Check each option
greet(); usesgreet();which is correct. Others use invalid syntax.Final Answer:
greet(); -> Option DQuick Check:
Function call = name + () [OK]
- Omitting parentheses
- Using wrong keywords
- Using brackets instead of parentheses
function add(a, b) {
return a + b;
}
console.log(add(3, 4));Solution
Step 1: Understand function parameters and return
The functionaddtakes two numbers and returns their sum.Step 2: Calculate the return value for add(3, 4)
3 + 4 equals 7, so the function returns 7.Final Answer:
7 -> Option AQuick Check:
3 + 4 = 7 [OK]
- Treating numbers as strings
- Missing return statement
- Confusing output with parameters
function sayHi() {
console.log('Hi!');
}
sayHiSolution
Step 1: Check how the function is called
The functionsayHiis referenced but not called because parentheses are missing.Step 2: Understand function call syntax
To run the function, it must be called with parentheses:sayHi();Final Answer:
Missing parentheses to call the function. -> Option AQuick Check:
Function call needs () [OK]
- Forgetting parentheses
- Confusing function reference with call
- Assuming function runs automatically
function multiply(x, y) {
return x * y;
}
function calculate() {
let result = multiply(2, 3);
return result + 4;
}
console.log(calculate());What is the output and why?
Solution
Step 1: Trace multiply function call
Callingmultiply(2, 3)returns 2 * 3 = 6.Step 2: Calculate final result in calculate
The variableresultis 6, thenresult + 4equals 10.Final Answer:
10, because multiply returns 6 and 6 + 4 = 10. -> Option CQuick Check:
2*3=6; 6+4=10 [OK]
- Multiplying instead of adding final step
- Ignoring return values
- Assuming function not called
Ready to prove it?
Pass the test and this pattern moves unknown
