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 Random Quote Generator

Xu Cheng
Xu Cheng
5,038 Points

How to make a template in Project 1

For the "printQuote constructs a string containing the different properties of the quote object using the following HTML template" part, I am doing it manually, which is get the info from the object and add to the specific text/code we need, such as '<p class="quote">' + object[quote] + '</p>';

However I am wondering is there any faster way to do it?

I know we could do it using EJS and Node... But for simple Javascript and HTML, how to do it except adding all of them manually?

Thank you very much!

1 Answer

Steven Parker
Steven Parker
231,248 Points

I don't have access to the project since I'm not a Techdegree student, so I'm guessing a bit about what you're after. But if you just want to use a template string to employ interpolation, the string you wrote above could be rewritten as:

`<p class="quote"> ${object[quote]} </p>`

But I don't expect this is any faster, nor does it make the code much more compact in this case.

On the other hand, if you wanted to loop through an object and create a paragraph from each item with a class named the same as the key, you might do something like this:

myString = ""
for (let key in object) {
  myString += `<p class="${key}">${object[key]}</p>`;
}