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 trialSam Bailey
8,425 Pointsadding 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
231,268 PointsThe "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.