Reading user input with prompt()

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Reading user input with prompt()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line stores what the user types in a variable called name?
A let name = prompt("Your name?");
B prompt("Your name?") = name;
2. A user types 14 into prompt("Age?"). What data type does prompt() return?
A A number: 14
B A string: "14"
3. What does prompt("Favourite subject?") do when it runs?
A Shows a message box, waits for typing and returns the text
B Prints the message in the console and carries on straight away
4. What does prompt() return if the user clicks Cancel?
A An empty number 0
B null
5. let a = prompt("First?"); with the user typing 5 and then 3 in a second prompt stored in b. What does a + b give?
A "53" because both are strings
B 8 because they are numbers

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1let name = prompt("Your name?"); Aa is a string, so a + 5 joins text instead of adding. If the user types 3 the result is "35". Use Number(prompt(...)).
2let age = prompt("Your age?"); console.log(typeof age); Bif (answer === null) { console.log("Cancelled"); }
3prompt() returns null when the user presses Cancel, so you can check for it CShows a message box, waits for the user to type, and stores what they typed in the variable name
4Input is a string, so convert it with Number() before doing maths Dlet n = Number(prompt("Pick a number"));
5prompt("Your name?"); console.log("Hi " + name); EPrints string, because prompt() always returns text, even when the user types digits
6let a = prompt("First number?"); let total = a + 5; FThe answer was never stored in a variable, so name does not exist. Use let name = prompt("Your name?");

Definitions

prompt("question")

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
let name = ______("What is your name?");
2.
let name = prompt("Your name?"); console.log("Hi " + ____);
3.
let age = prompt("Age?"); console.log(______ age);
4.
let name = prompt("Your name?"); if (name === ____) { console.log("Cancelled"); }
5.
let num = ______(prompt("Enter a number:"));

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A // ask for a name
B let name = promt("Name?");
C console.log("Hi " + name);
Question 2
A let age = prompt("Age?");
B let next = Number(age) + 1;
C console.log("Next: " + nex);
Question 3
A let n = prompt("Number?");
B let total = Number(n) + 5;
C console.log("Total: " + total;
Question 4
A let ans = prompt("Number?");
B let n = Number(ans);
C let total = n + 5;
D console.log("Sum:", ans + 5);
Question 5
A let city = prompt(City?);
B console.log("Hello " + city);
C console.log(city.length);

Answer Key

Reading user input with prompt()
Section A: Quick Questions
Q1
A — let name = prompt("Your name?");
Q2
B — A string: "14"
Q3
A — Shows a message box, waits for typing and returns the text
Q4
B — null
Q5
A — "53" because both are strings
Section B: Match the Pairs
Q1
let name = prompt("Your name?"); → Shows a message box, waits for the user to type, and stores what they typed in the variable name
Q2
let age = prompt("Your age?"); console.log(typeof age); → Prints string, because prompt() always returns text, even when the user types digits
Q3
prompt() returns null when the user presses Cancel, so you can check for it → if (answer === null) { console.log("Cancelled"); }
Q4
Input is a string, so convert it with Number() before doing maths → let n = Number(prompt("Pick a number"));
Q5
prompt("Your name?"); console.log("Hi " + name); → The answer was never stored in a variable, so name does not exist. Use let name = prompt("Your name?");
Q6
let a = prompt("First number?"); let total = a + 5; → a is a string, so a + 5 joins text instead of adding. If the user types 3 the result is "35". Use Number(prompt(...)).
Section C: Definitions
Q1
prompt("question") — Shows a message box, waits for the user to type, then returns what they typed as a string. Store it in a variable. Cancel returns null.
Section D: Fill in the Blanks
Q1
let name = prompt("What is your name?");
Q2
let name = prompt("Your name?"); console.log("Hi " + name);
Q3
let age = prompt("Age?"); console.log(typeof age);
Q4
let name = prompt("Your name?"); if (name === null) { console.log("Cancelled"); }
Q5
let num = Number(prompt("Enter a number:"));
Section E: Spot the Error
Q1
B — The function name is misspelled. It must be prompt, with the letter p before t, otherwise JavaScript throws a ReferenceError.
Q2
C — The variable is called next, but this line uses nex, which was never declared. It should be next.
Q3
C — The closing bracket of console.log( is missing. It should end with total);
Q4
D — prompt() always returns a string, so ans + 5 joins the text, for example "35" instead of 8. Use the converted number: total (or n + 5).
Q5
A — The question inside prompt() is text, so it needs quotation marks: prompt("City?").