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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2 Solution

J V
J V
3,414 Points

Trying to review the code for better understanding Two-Dimensional Arrays

I am just trying to review the code for better understanding in relation to how the code is displayed on the screen:

Below is a small portion of code form the Build a Quiz using Two-Dimensional Arrays:

HTML = "You got these answers correct: " + correctQ + "<br>";
HTML += "<h2>You got these questions correct: </h2>";
HTML += buildList(rightQuestions);

HTML += "<h2>You got these questions incorrect: </h2>";
HTML += buildList(wrongQuestions);

Is it accurate to say the above code is the same as this code below (refer to code that is not commented out):

/*Writing the code out for visualization
HTML = "You got these answers correct: " + correctQ + "<br>";

HTML = HTML + "<h2>You got these questions correct: </h2>";
HTML = HTML + buildList(rightQuestions);

HTML = HTML + "<h2>You got these questions incorrect: </h2>";
HTML = HTML + buildList(wrongQuestions);
*/


HTML = "You got these answers correct: " + correctQ + "<br>";
HTML =  "You got these answers correct: " + correctQ + "<br>" +
               "<h2>You got these questions correct: </h2>";

HTML = "You got these answers correct: " + correctQ + "<br>" + 
              "<h2>You got these questions correct: </h2>" +
              "<ol>" + "<li>" + array[i] + "</li>" + "</ol>";


HTML = HTML + "<h2>You got these questions incorrect: </h2>";

HTML = "<h2>You got these questions incorrect: </h2>" + 
             "<ol>" + "<li>" + array[i] + "</li>" + "</ol>";

Also, is there an easier way to display all of this code instead of writing HTML += ..etc?

2 Answers

The two examples are not the same at all, due to the assignment operators used "+=" and "=". In the first example by using the += operator you are adding the value right of the operator into the variable in other words concatenating into the variable. In the second example you are using just the "=" operator. By doing this you are not adding the value, rather you are just assigning a new value to the variable, not concatenating them.

I hope this helps.

I'm not sure about anything better than using the += operator except a using a loop perhaps.

if you want to learn more check this out:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Addition_assignment

J V
J V
3,414 Points

Hi Jacob,

Thank you for your input. I appreciate it. Thanks for the reference link as well. :)