Back

Activity 1.7: Console Input

divider

Activity 1.7

Console Input

Key Concepts

prompt()

alert()

Getting Input

prompt() asks the user a question and gives back whatever they type as a string.

let name = prompt("Enter your name:");
console.log(`Hello ${name}!`);

Pausing with alert()

When a program runs several demos back to back,alert() pauses execution until the user presses enter — giving them a chance to read the output before it moves on.

console.log("Section 1");
alert("Press enter to continue");
console.log("Section 2");

Today's Objectives

  • Gathering input from the user with prompt()
  • Interpolating that input into output
  • Pausing between demos with alert()

Key Terms

prompt()
Asks the user a question and returns what they typed, as a string.
alert()
Pauses the program until the user presses enter.

'F' → Fullscreen

divider

Build

Create a new JavaScript file named 1-7-console-input.js.


Task 1: Simple To-Do List

  • Ask the user for three tasks.
  • Print them back as a numbered list.
1-7-console-input.js
console.log("--- Demo 1 - Simple To-Do List ---");
console.log("Let's add a few tasks to your list.");
let task1 = prompt("Task 1:");
let task2 = prompt("Task 2:");
let task3 = prompt("Task 3:");
console.log();
console.log("Your To-Do List:");
console.log(`1. ${task1}`);
console.log(`2. ${task2}`);
console.log(`3. ${task3}`);
alert("Press enter to continue...");

Task 2: User Profile Setup

  • Ask the user for a username, favorite subject, and after-school activity.
  • Print a short profile summary.
1-7-console-input.js
console.log("--- Demo 1 - Simple To-Do List ---");
console.log("Let's add a few tasks to your list.");
let task1 = prompt("Task 1:");
let task2 = prompt("Task 2:");
let task3 = prompt("Task 3:");
console.log();
console.log("Your To-Do List:");
console.log(`1. ${task1}`);
console.log(`2. ${task2}`);
console.log(`3. ${task3}`);
alert("Press enter to continue...");
console.log("--- Demo 2 - User Profile Setup ---");
console.log("WELCOME! LET'S SET UP YOUR PROFILE...");
let username = prompt("Enter your username:");
let subject = prompt("What's your favorite subject in school?");
console.log("Choose your preferred after-school activity:");
console.log("- Sports");
console.log("- Music");
console.log("- Gaming");
console.log("- Volunteering");
let activity = prompt("->");
console.log();
console.log("- Creating your profile -");
console.log(`NAME: ${username}`);
console.log(`FAVORITE SUBJECT: ${subject}`);
console.log(`AFTER-SCHOOL ACTIVITY: ${activity}`);
alert("Press enter to continue...");

Task 3: Simple Survey

  • Ask the user three survey questions.
  • Print the results.
1-7-console-input.js
console.log("--- Demo 1 - Simple To-Do List ---");
console.log("Let's add a few tasks to your list.");
let task1 = prompt("Task 1:");
let task2 = prompt("Task 2:");
let task3 = prompt("Task 3:");
console.log();
console.log("Your To-Do List:");
console.log(`1. ${task1}`);
console.log(`2. ${task2}`);
console.log(`3. ${task3}`);
alert("Press enter to continue...");
console.log("--- Demo 2 - User Profile Setup ---");
console.log("WELCOME! LET'S SET UP YOUR PROFILE...");
let username = prompt("Enter your username:");
let subject = prompt("What's your favorite subject in school?");
console.log("Choose your preferred after-school activity:");
console.log("- Sports");
console.log("- Music");
console.log("- Gaming");
console.log("- Volunteering");
let activity = prompt("->");
console.log();
console.log("- Creating your profile -");
console.log(`NAME: ${username}`);
console.log(`FAVORITE SUBJECT: ${subject}`);
console.log(`AFTER-SCHOOL ACTIVITY: ${activity}`);
alert("Press enter to continue...");
console.log("--- Demo 3 - Simple Survey ---");
console.log("We'd love to get your feedback!");
let favoriteColor = prompt("What is your favorite color?");
let favoriteFood = prompt("What is your favorite food?");
let travelDestination = prompt("Where is a place you would like to travel?");
console.log();
console.log("- Survey Results -");
console.log(`Favorite Color: ${favoriteColor}`);
console.log(`Favorite Food: ${favoriteFood}`);
console.log(`Travel Destination: ${travelDestination}`);
alert("Press enter to continue...");

Task 4: Fantasy Story Builder

  • Ask the user for a main character, a magical creature, and a magical place.
  • Interpolate their answers into a short story.
1-7-console-input.js
console.log("--- Demo 1 - Simple To-Do List ---");
console.log("Let's add a few tasks to your list.");
let task1 = prompt("Task 1:");
let task2 = prompt("Task 2:");
let task3 = prompt("Task 3:");
console.log();
console.log("Your To-Do List:");
console.log(`1. ${task1}`);
console.log(`2. ${task2}`);
console.log(`3. ${task3}`);
alert("Press enter to continue...");
console.log("--- Demo 2 - User Profile Setup ---");
console.log("WELCOME! LET'S SET UP YOUR PROFILE...");
let username = prompt("Enter your username:");
let subject = prompt("What's your favorite subject in school?");
console.log("Choose your preferred after-school activity:");
console.log("- Sports");
console.log("- Music");
console.log("- Gaming");
console.log("- Volunteering");
let activity = prompt("->");
console.log();
console.log("- Creating your profile -");
console.log(`NAME: ${username}`);
console.log(`FAVORITE SUBJECT: ${subject}`);
console.log(`AFTER-SCHOOL ACTIVITY: ${activity}`);
alert("Press enter to continue...");
console.log("--- Demo 3 - Simple Survey ---");
console.log("We'd love to get your feedback!");
let favoriteColor = prompt("What is your favorite color?");
let favoriteFood = prompt("What is your favorite food?");
let travelDestination = prompt("Where is a place you would like to travel?");
console.log();
console.log("- Survey Results -");
console.log(`Favorite Color: ${favoriteColor}`);
console.log(`Favorite Food: ${favoriteFood}`);
console.log(`Travel Destination: ${travelDestination}`);
alert("Press enter to continue...");
console.log("--- Demo 4 - Fantasy Story Builder ---");
console.log("Let's create a fantasy story!");
let mainCharacter = prompt("Name your main character:");
let magicalCreature = prompt("Name a magical creature:");
let aMagicalPlace = prompt("Name a magical place:");
console.log();
console.log("- Your Story -");
console.log(`Once upon a time, there was a hero named ${mainCharacter}.`);
console.log(`${mainCharacter} traveled to the ${aMagicalPlace} to seek the legendary ${magicalCreature}.`);
console.log("Their quest was just beginning...");
divider

Checkpoint

Verify your program works correctly.

Example Output
--- Demo 1 - Simple To-Do List ---
Let's add a few tasks to your list.
Task 1: Do Homework
Task 2: Cook Dinner
Task 3: Clean Bedroom
Your To-Do List:
1. Do Homework
2. Cook Dinner
3. Clean Bedroom
Press enter to continue... [Enter]
--- Demo 2 - User Profile Setup ---
WELCOME! LET'S SET UP YOUR PROFILE...
Enter your username: amortimer
What's your favorite subject in school? Computer Science
Choose your preferred after-school activity:
- Sports
- Music
- Gaming
- Volunteering
-> Gaming
- Creating your profile -
NAME: amortimer
FAVORITE SUBJECT: Computer Science
AFTER-SCHOOL ACTIVITY: Gaming
Press enter to continue... [Enter]
--- Demo 3 - Simple Survey ---
We'd love to get your feedback!
What is your favorite color? purple
What is your favorite food? pizza
Where is a place you would like to travel? Japan
- Survey Results -
Favorite Color: purple
Favorite Food: pizza
Travel Destination: Japan
Press enter to continue... [Enter]
--- Demo 4 - Fantasy Story Builder ---
Let's create a fantasy story!
Name your main character: Ada
Name a magical creature: Griffin
Name a magical place: Cleveland
- Your Story -
Once upon a time, there was a hero named Ada.
Ada traveled to the Cleveland to seek the legendary Griffin.
Their quest was just beginning...
divider

Reflection

Answer the following questions before submitting your work.

  1. What does prompt() give back to your program — and what type of value is it, always?
  2. Why does this program use alert() between demos instead of just letting all four run back to back?
  3. Pick one of the four demos. What's one other question you could add to make it more interesting?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete