Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2 Solution

adding in "var outputDiv" method only allows me to run the print(message) function once.

I used a different method to output the results of the quiz onto the page, where I print 3 variables I have created to the page. The output should be the same as the one as the video, and indeed it was all working perfectly until I substituted the "document.write(message)" method for the "outputDiv.innerHTML = message" that Dave suggested.

My code snapshot: https://w.trhou.se/utbyuzzjjc

The three variables I want to print to the page are at the bottom of my code snapshot ("scoreTotal", "questionsRight" and "questionsWrong", for reference). But when I use the outputDiv.innerHTML method, only the third one (print(questionsWrong) is outputted.

I tested this by removing each print function call one by one, and it always prints the last one . Yet, if I swap back to using document.write, it prints all 3 as expected. Can anyone explain why this is the case?

(please ignore the additional code at the bottom of the page, as this was just my experimentation).

1 Answer

Steven Parker
Steven Parker
210,487 Points

The "document.write" method adds text to the page, but the assignment operator replaces anything already there with the value being assigned.

One fix would be to combine all the text into a large string and then assign that, or to use a concatenating assignment ("+=") instead of a normal assignment every time other than the first.