Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ismael Ramirez
3,627 PointsWhat's wrong with my code?
var noun = prompt("Name a noun"); var verb = prompt("Name a verb"); var adjective = prompt("Name an adjective"); alert("Are you ready to see the sentence you created?"); var sentence = "<h1>noun + ' verb + ' adjective + ' ceviche. </h1>"; document.write(sentence);
the finished sentence says; noun + ' verb + ' adjective + ' ceviche.
1 Answer
Matthew Lanin
Full Stack JavaScript Techdegree Student 8,003 PointsRight now, you're declaring the sentence variable as a string that contains -
<h1>noun + ' verb + ' adjective + ' ceviche. </h1>
If you declare a variable, and the content of the variable is between a set of quotation marks, you're telling the interpreter that you're declaring a string, and the information within said quotation marks is the content of the string.
In order to add variables to the mix, you can concatenate them. To do that, we put our HTML elements within quotation marks, along with any words or formatting (such as whitespace or punctuation) we would want, while the variables would be left outside of quotation marks so that they are read as variables, and join each piece with plus signs. So, something like this.
var sentence = "<h1>" + noun + " " + verb + " " + adjective + " ceviche. </h1>";
You'll learn this soon, but if you want to know a way that's even quicker to write and, IMO, easier to read, you could use a template literal. You'll learn about these in the near future, but our sentence variable from before would basically just look like this.
var sentence = `<h1>${noun} ${verb} ${adjective} ceviche.</h2>`;
All we do here is wrap everything in a set of backticks ( ` ), wrap the content between our HTML tags, write it out like a normal sentence, but for each variable, we write it with the dollar sign and curly braces, so ${variableName}.
Hope that helps, keep up the good work.
Ismael Ramirez
3,627 PointsIsmael Ramirez
3,627 Pointsthanks so much it worked