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

2 Answers

Steven Parker
Steven Parker
231,275 Points

One of the differences I noticed is this line:

else if( response !== answer )

The "if" isn't needed here since the condition is exact opposite of the one previously tested. So the "else" does the job by itself and keeps the code more compact.

Also, the video code includes a "buildList" function to format the questions in the final display. Passing the array directly to the "print" function won't give you the same bullet-indented separate lines.

Kjetil-Lennart A. Lorentzen
Kjetil-Lennart A. Lorentzen
13,390 Points

Thanks, i appreciate the answer. So he made it to be able to put everything in li elements, really?

Steven Parker
Steven Parker
231,275 Points

Did you update your snapshot? I didn't recall the list item tags being added before.

That does give you the same output, but using a function to create the list elements helps keep the code compact and satisfies the "DRY" principle ("Don't Repeat Yourself").

Kjetil-Lennart A. Lorentzen
Kjetil-Lennart A. Lorentzen
13,390 Points

Yet again, thanks for your response, you should be paid for this! I really appreciate it. https://w.trhou.se/kiz3tt5mh1 There's a new snapshot, only removed the if else. I reviewed his video, his does not check if all or none of the questions were correct, i didn't want a redundant "These were your wrong answers" or "These were your right answers" header if it wasn't needed. And i see that i made li elements too, haha, forgot! Anyway, how would i go about making my code more compact and keeping the if statements?

Steven Parker
Steven Parker
231,275 Points

It would be easier to answer your question if you pointed out specific areas of the code you are unsure about and/or the ways that your implementation differs from the example solution given in the video.

Kjetil-Lennart A. Lorentzen
Kjetil-Lennart A. Lorentzen
13,390 Points

I'm sorry for not being more specific, thanks for your answer

He made a function for making list items with a loop, here's the link to the video : https://teamtreehouse.com/library/javascript-loops-arrays-and-objects/tracking-multiple-items-with-arrays/build-a-quiz-challenge-part-2-solution And then started using that too build the list at the end. I used two if-statements instead, and i'm wondering if my method is bad practice, is it less reusable, how is that worse than his method? I'm ofcourse expecting his way to be best practice, the most reusable and handy, haha.

Steven Parker
Steven Parker
231,275 Points

You could use the "buildList" function like in the video, and still get your enhanced functionality by adding the "if" statements. But you probably would want to remove lines 35 and 42 since they only try to print empty lists.

Kjetil-Lennart A. Lorentzen
Kjetil-Lennart A. Lorentzen
13,390 Points

I will experiment with buildlists and print-functions. Yes! I have no idea why they are there, haha! Thank you very much Steven, you've cleared some stuff up for me