Output formatting basics in Javascript - Time & Space Complexity
When we format output in JavaScript, we often use loops or repeated steps to build the final result.
We want to know how the time it takes grows as the amount of data to format grows.
Analyze the time complexity of the following code snippet.
const numbers = [1, 2, 3, 4, 5];
let result = '';
for (const num of numbers) {
result += `Number: ${num}\n`;
}
console.log(result);
This code builds a string by adding a formatted line for each number in the array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the array and adding formatted text.
- How many times: Once for each number in the input array.
As the number of items grows, the loop runs more times, adding more lines to the output.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 string additions |
| 100 | 100 string additions |
| 1000 | 1000 string additions |
Pattern observation: The work grows directly with the number of items; double the items, double the work.
Time Complexity: O(n)
This means the time to format output grows in a straight line with the number of items.
[X] Wrong: "Adding more items won't affect the time much because it's just a simple string."
[OK] Correct: Each item adds a new step, so more items mean more work and more time.
Understanding how output formatting scales helps you write efficient code that handles bigger data smoothly.
"What if we used nested loops to format pairs of items? How would the time complexity change?"
Practice
name inside a string using template literals in JavaScript?Solution
Step 1: Understand template literals syntax
Template literals use backticks (`) and ${} to insert variables inside strings.Step 2: Identify correct usage
console.log(`Hello, ${name}!`); uses backticks and ${name} correctly to embed the variable.Final Answer:
console.log(`Hello, ${name}!`); -> Option AQuick Check:
Backticks + ${} = correct template literal [OK]
- Using quotes instead of backticks for template literals
- Forgetting ${} around variable
- Using single or double quotes with ${} which won't interpolate
Solution
Step 1: Check the use of template literals
Backticks allow strings with variables inside ${}, but here no variable is needed.Step 2: Identify correct string
console.log(`5 apples`); correctly uses backticks with plain text '5 apples'.Final Answer:
console.log(`5 apples`); -> Option AQuick Check:
Backticks with plain text = valid string [OK]
- Using quotes instead of backticks when template literals expected
- Trying to insert undefined variable apples
- Adding plus sign inside template literal as text
const count = 3;
console.log(`You have ${count} new messages.`);
Solution
Step 1: Understand template literal variable insertion
The variablecountinside ${} is replaced by its value 3.Step 2: Evaluate the console.log output
The string becomes 'You have 3 new messages.' and prints exactly that.Final Answer:
You have 3 new messages. -> Option CQuick Check:
Variable inside ${} = replaced by value [OK]
- Thinking ${count} prints literally
- Confusing quotes and backticks
- Expecting variable name instead of value
let user = 'Anna';
console.log('Welcome, ${user}!');
Solution
Step 1: Check string quotes for interpolation
Single quotes ('') do not process ${} as variable placeholders.Step 2: Identify correct string type for template literals
Backticks (`) are needed to enable variable interpolation.Final Answer:
Single quotes do not allow variable interpolation -> Option BQuick Check:
Use backticks for ${} interpolation [OK]
- Thinking single quotes support ${} interpolation
- Ignoring missing backticks
- Assuming variable is undeclared
name = 'Sam' and age = 25. Which code correctly formats the output as: Hello, Sam! You are 25 years old.Solution
Step 1: Use template literals with variables
Backticks and ${} allow embedding variablesnameandageinside the string.Step 2: Check each option for correct variable usage
console.log(`Hello, ${name}! You are ${age} years old.`); correctly uses ${name} and ${age} to insert values.Final Answer:
console.log(`Hello, ${name}! You are ${age} years old.`); -> Option DQuick Check:
Backticks + ${var} for each variable = correct [OK]
- Using variable names without ${} inside template literals
- Mixing quotes and backticks incorrectly
- Concatenating strings instead of using template literals
Ready to prove it?
Pass the test and this pattern moves unknown
