Code Challenge: Age Predictor
From Bold Idea Knowledgebase
Solving the Age Predictor code challenge demonstrates students understanding of variable re-assignment and strings .
Requirements
When run, the program must do the following:
- Ask for the user’s name - Greet them by name - Ask for the user’s age - Tell the user how old they’ll be in 50 years
Solution
- The first fix is line 8, which says `alert("Hello, name!")`. Replace the greeting with one that uses a variable in place of their name. Be sure to use backticks instead of quotes for the string (backticks allow you to use variables inside a string).
- On line 11, the age must be converted to a Number
- On line 14, futureAge must be set to `age + 40`.
- On line 17, because the string has a variable in it, you must replace the quotes with backticks
Example Solution
// Get the name from the user
let name = prompt("Enter your name: ");
// Greet the user
alert(`Hello, ${name!}`);
// Get the age from the user
let age = Number(prompt("How old are you?:"));
// Calculate the age in 50 years
let futureAge = age + 50;
// Tell the user how old they'll be in 50 years
alert(`In 50 years you will be ${futureAge} years old.`);
Alternative Solution
// Get the name from the user
let name = prompt("Enter your name: ");
// Greet the user
alert(`Hello, ${name!}`);
// Get the age from the user
let age = prompt("How old are you?:");
// Calculate the age in 50 years
let futureAge = Number(age) + 50;
// Tell the user how old they'll be in 50 years
alert(`In 50 years you will be ${futureAge} years old.`);