0
0
Javascriptprogramming~20 mins

Basic input concepts (prompt overview) in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this prompt input code?
Consider this JavaScript code that asks the user for their name and then greets them. What will be shown in the console if the user enters Alex?
Javascript
const name = prompt('What is your name?');
console.log(`Hello, ${name}!`);
AHello, name!
BHello, Alex!
CWhat is your name?
Dundefined
Attempts:
2 left
💡 Hint
The prompt function returns the text the user types.
Predict Output
intermediate
2:00remaining
What happens if the user presses Cancel in prompt?
Look at this code snippet. What will be the value of answer if the user presses Cancel in the prompt box?
Javascript
const answer = prompt('Type something or press Cancel');
console.log(answer);
AThrows an error
B"" (empty string)
Cundefined
Dnull
Attempts:
2 left
💡 Hint
Cancel returns a special value different from empty string.
🧠 Conceptual
advanced
2:00remaining
Why should you convert prompt input to a number?
The prompt function always returns a string. Why is it important to convert this string to a number when expecting numeric input?
ABecause strings cannot be stored in variables
BBecause prompt returns undefined for numbers
CBecause arithmetic operations on strings can produce unexpected results
DBecause numbers are always returned as null
Attempts:
2 left
💡 Hint
Think about what happens if you add two strings that look like numbers.
Predict Output
advanced
2:00remaining
What is the output of this code with numeric input?
What will be the output if the user enters 5 in the prompt?
Javascript
const input = prompt('Enter a number');
const result = input * 2;
console.log(result);
A10
B"55"
Cundefined
DNaN
Attempts:
2 left
💡 Hint
Multiplying a string that looks like a number by a number converts it automatically.
Predict Output
expert
2:00remaining
What error does this code produce?
What error will this code cause when run in a browser?
Javascript
const name = prompt('Enter your name');
alert('Hello, ' + name.toUpperCase());
ATypeError: Cannot read property 'toUpperCase' of null
BSyntaxError: Unexpected token
CReferenceError: name is not defined
DNo error, alerts 'Hello, NAME'
Attempts:
2 left
💡 Hint
What happens if the user presses Cancel and name is null?