Challenge - 5 Problems
Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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}!`);
Attempts:
2 left
💡 Hint
The prompt function returns the text the user types.
✗ Incorrect
The prompt function shows a box asking for input. The input is stored in the variable 'name'. Then console.log prints 'Hello, ' plus the input plus '!'. So if the user types 'Alex', the output is 'Hello, Alex!'.
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Cancel returns a special value different from empty string.
✗ Incorrect
When the user presses Cancel, prompt returns null, which is logged to the console.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Think about what happens if you add two strings that look like numbers.
✗ Incorrect
If you don't convert the string to a number, adding two inputs will concatenate them as strings instead of adding numerically.
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
Multiplying a string that looks like a number by a number converts it automatically.
✗ Incorrect
JavaScript converts the string '5' to number 5 when using the * operator, so 5 * 2 = 10.
❓ Predict Output
expert2: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());
Attempts:
2 left
💡 Hint
What happens if the user presses Cancel and name is null?
✗ Incorrect
If the user presses Cancel, prompt returns null. Calling toUpperCase() on null causes a TypeError.