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 trialJames Falconi
5,375 PointsWhat unique thing is happening within the "for" loop inside the displayChoices function (within the QuizUI object)?
I haven't seen the use of the "+ i" followed by "choices[i]." I see how it is using the parameters for the respective referenced functions, but it would never occur to me to use this syntax.
for(var i = 0; i < choices.length; i++) { this.populateIdWithHTML("choice" + i, choices[i]); this.guessHandler("guess" + i, choices[i]); }
2 Answers
David Bath
25,940 PointsThis way of retrieving the elements may not be the best way to do it, but it works well enough because each ID that you need to retrieve uses an expected format which can use the loop's iterator as part of the ID. So the populateIdWithHTML
method's first parameter will be something like choice0
, choice1
, etc. which the method will use as an ID to select an element on the page with that ID, and then the second parameter is the value in choices[0]
, choices[1]
, etc. which will populate the selected element.
James Falconi
5,375 PointsThanks, David! I had an inkling it had something to with that. I had thought about the parameters and it didn't sink in at first. But, your explanation helped me get there. Thank you for your response.