I’m working on code that asks the user for their name and age.
var name;
var age;
name = prompt("What is your name?");
age = prompt("What is your age?");
The code is setup to repeat their name and age back to them, and then it uses an if statement to either send them a message saying “You’re young” if the age variable was under 50.
I have a problem, though. I can’t print the string that tells that user what the age and name they inputted was. I get a syntax error that tells me there’s an unexpected string.
The problem code is this:
console.log("You're name is " +name " and you are " +age "years old.");
The code works fine if I only print one variable to the console, like this:
console.log("You're name is " +name.);
Hopefully you guys can help me out.
Full code:
var name;
var age;
name = prompt("What is your name?");
age = prompt("What is your age?");
console.log("You're name is " +name " and you are " +age "years old.")
var printNameAndAge = function() {
if (age>50) {
console.log("Dang you're old.");
}
else {
console.log("You're pretty young, "+name);
alert("You're pretty young, "+name);
}
};
printNameAndAge();
To insert a variable inline, you need to use the concatenation operator both before and after.