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

Ygor Fonseca
Ygor Fonseca
6,929 Points

innerHTML don't work

Hey Guys...

I just finished the array section of the "Javascript, Arrays and Objects" course, and the final challenge was pretty hard but i find out how solve it...

The problem is after I replace the "document.write" for "innerHTML" in the "print function" the code don't work the way as I want, someone can help me with this?

Here is the code: https://w.trhou.se/xi9978nwwh

1 Answer

rydavim
rydavim
18,813 Points

When you call innerHTML, you're setting the html for that element. This means that your inner html will only be equal to the last thing to set it to. Basically, you're overwriting your html repeatedly with new information, instead of adding information.

// Change all of these print statements to add to the message variable.
// Then print the message variable.

print("<h2>You Hit " + rightHits + " Questions:</h2>");
print(generateAnswersList(rightQuestions));

print("<h2>And Missed " + wrongHits + " Questions:</h2>");
print(generateAnswersList(wrongQuestions));
// Something like this...

var message = "<h2>You Hit " + rightHits + " Questions:</h2>";
message += generateAnswersList(rightQuestions);

message += "<h2>And Missed " + wrongHits + " Questions:</h2>";
message += generateAnswersList(wrongQuestions);

print(message);
Ygor Fonseca
Ygor Fonseca
6,929 Points

Thank you so much, your solution worked pretty well...