Getting this error

var dogAge = prompt("What is your dog's age?");
var humanAge = ((dogAge - 2) *4) + 21

alert("Your dog is " + humanAge " human years old");
var dogAge = prompt("What is your dog's age?");
var humanAge = ((dogAge - 2) *4) + 21

alert("Your dog is " + humanAge " human years old");
This is the code. I've tried this in vscode as well and prettier starts giving another error in my var humanAge and says it needs a
,
,
wtf.
3 Replies
Coder_Carl
Coder_Carl2y ago
missing a + in the string interpolation after humanage variable reference also unsure how the * next to a number is treated 🤔
Jochem
Jochem2y ago
should just be multiplication
WillsterJohnson
this is part of the pain of concatenating strings, it's really easy to miss a + sign. You should have a + after humanAge, so humanAge + " human ... It's typically easier to use template literals, a fancy name for a really simple syntax. Say I want to let a user enter a name, then I output hello, followed by their name. I could do this;
const name = getUserName(); // imaginary function
console.log("hello, " + name);
const name = getUserName(); // imaginary function
console.log("hello, " + name);
But I have to make sure I add in the space at the end of the string, and the plus sign. It's easier to use template literals, instead of a " or ' for the string, use a backtick ` (it's to the left of the 1 key)
const name = getUserName(); // again, just imaginary
console.log(`hello, ${name}`);
const name = getUserName(); // again, just imaginary
console.log(`hello, ${name}`);
Template literals use this ${contentHere} syntax, you can put any code in there you could put in a function argument You can do fun stuff like ternary operations, string manipulation, etc.
function greet(name, lang="english") {
return `${lang === "english" ? "hello" : "bonjour"}, ${name.toUpperCase()}!`
}
function greet(name, lang="english") {
return `${lang === "english" ? "hello" : "bonjour"}, ${name.toUpperCase()}!`
}
Pretty much, you can put anything in there so long as it resolves to a single value, so IIFEs are valid, but for loops are not. MDN docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals James Q Quick on Template Literals: https://youtu.be/O4DaRScJFNs?t=19