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

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables The Variable Challenge Solution

Yemaya Re
PLUS
Yemaya Re
Courses Plus Student 1,922 Points

Variable Challenge: What are your thoughts on my code? I'm very new to JS!

var adj = prompt('Begin by typing an adjective');

var noun = prompt('Next, add a noun');

var verb = prompt('Finally add a verb');

alert('Great! Are you ready to read your unique story?');

var sentence = "The amazing ";

sentence += adj + " ";

sentence += noun + " told me YES, I can ";

sentence += verb + " my way into the hearts of millions!! ";

sentence += " Once I said my peace, the men in white coats took me into the quiet room.";

document.write('<h2>' + sentence +'</h2>');

2 Answers

Valeshan Naidoo
Valeshan Naidoo
27,008 Points

Yeah that works, but you can see how tedious it can be by constantly concatenating each component, there is a workshop about using backticks (`) instead of (") or ('), and with that you can do this.

var adj = prompt('Begin by typing an adjective');

var noun = prompt('Next, add a noun');

var verb = prompt('Finally add a verb');

alert('Great! Are you ready to read your unique story?');

var sentence = `The amazing ${adj} ${noun} told me YES, I can ${verb} my way into the hearts of millions!! Once I said my peace, the men in white coats took me into the quiet room.`;

document.write(sentence);
Yemaya Re
Yemaya Re
Courses Plus Student 1,922 Points

Hmm ok, your code looks much easier to work with/write. Is the backtick workshop in this Javascript track? Thank you for your feedback!

Valeshan Naidoo
Valeshan Naidoo
27,008 Points

Yep, it's part of the javascript track, the introducing template literals workshop. You're welcome :)