Basic input concepts (prompt overview) in Javascript - Time & Space Complexity
When we ask a program to get input from a user, it waits for the user to type something. We want to understand how this waiting affects the program's speed.
How does the time the program takes change as the input size changes?
Analyze the time complexity of the following code snippet.
const userInput = prompt("Enter your name:");
console.log(`Hello, ${userInput}!`);
This code asks the user to type their name and then greets them using what they typed.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Waiting for user input once.
- How many times: Exactly one time per program run.
When the program waits for input, the time depends on how long the user takes to type, but the program itself does not do extra work as input size grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 characters | 1 wait + 1 print |
| 100 characters | 1 wait + 1 print |
| 1000 characters | 1 wait + 1 print |
Pattern observation: The program waits once, no matter how long the input is, so the work does not grow with input size.
Time Complexity: O(1)
This means the program's work stays the same no matter how big the input is because it only waits once for the user to type.
[X] Wrong: "The program takes longer because it has to process each character typed."
[OK] Correct: The program just waits for the input once and then uses it; it does not do extra work for each character typed.
Understanding how input affects program speed helps you explain how programs handle user data efficiently and shows you can think about real-world program behavior.
"What if the program asked for input inside a loop that runs n times? How would the time complexity change?"
Practice
prompt() function do in JavaScript?Solution
Step 1: Understand the purpose of
Theprompt()prompt()function shows a dialog box that asks the user to type something.Step 2: Identify the type of input it collects
The input collected is always text (a string), even if the user types numbers.Final Answer:
It asks the user to enter some input as text. -> Option DQuick Check:
prompt() = asks user input [OK]
- Thinking prompt() shows output instead of input
- Assuming prompt() returns numbers automatically
- Confusing prompt() with console.log()
prompt()?Solution
Step 1: Check the correct function name and usage
The function to get input isprompt(), notinput()orconsole.prompt().Step 2: Assign the result to a variable
We store the user's input in a variable usinglet name = prompt('Enter your name');.Final Answer:
let name = prompt('Enter your name'); -> Option AQuick Check:
Correct prompt syntax = let name = prompt('Enter your name'); [OK]
- Using input() instead of prompt()
- Trying to assign prompt as a string
- Using console.prompt() which doesn't exist
25 in the prompt?let age = prompt('Enter your age');
console.log(typeof age);Solution
Step 1: Understand the type returned by prompt()
Theprompt()function always returns a string, even if the user types a number.Step 2: Check the output of
Sincetypeof ageageholds a string,typeof agewill be "string".Final Answer:
"string" -> Option AQuick Check:
prompt() returns string type [OK]
- Assuming prompt returns a number automatically
- Confusing typeof output with input value
- Expecting undefined or object type
let number = prompt('Enter a number');
let result = number + 10;
console.log(result);Solution
Step 1: Check the type of input from prompt()
Theprompt()returns a string, sonumberis a string.Step 2: Understand the effect of adding a number to a string
Adding 10 to a string causes string concatenation, not numeric addition.Final Answer:
The input from prompt() is a string, so adding 10 concatenates instead of sums. -> Option BQuick Check:
String + number = concatenation [OK]
- Thinking prompt input is a number by default
- Ignoring type conversion before arithmetic
- Believing console.log can't print variables
prompt() and add 10 to it? Choose the correct code.Solution
Step 1: Convert prompt input to a number
UseNumber()orparseInt()to convert the string input to a number.Step 2: Add 10 to the converted number
After conversion, adding 10 performs numeric addition correctly.Final Answer:
let num = Number(prompt('Enter number')) + 10; -> Option CQuick Check:
Convert input to number before adding [OK]
- Adding number directly to prompt() string
- Forgetting to call prompt() as a function
- Using parseInt without parentheses
Ready to prove it?
Pass the test and this pattern moves unknown
