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 trialVolodymyr Boretskyi
9,094 Pointsexplain please line 15 in quiz.js
what's in square brackets?
return this.questions[this.currentQuestionIndex];
3 Answers
liktid
6,387 PointsThis line returns the current question of your Quiz Object. this.currentQuestionIndex is starting with 0 (the first position of your array of questions) and with the first question it would look like this.questions[0]. If there are more questions in the array Quiz.prototype.guess adds 1 to this.currentQuestionIndex so now we would have this.questions[1] and this would be the second question in your questionsarray.
hope this helps
Christopher Stöckl
19,795 PointsHi Volodymyr!
"this" is quite hard to understand in the beginning, but once you do its quite simple :)
In this case "this" refers to the current instance of the object "Quiz".
The function that the line you gave us is in, should return the current question.
"questions" is an array of questions. Each question has an index. The first question has the index "0", the second index "1" and so forth...
So if you take part in the quiz and you are at question number 2 the variable "currentQuestionIndex" is 1.
What the line of code you printed then basically says is "Give me this quizzes question number 2".
or
return Quiz.questions[1];
I hope I explained it good enough :)
Volodymyr Boretskyi
9,094 PointsYes , thanks. I have hard times with all these substitutions when looping trough arrays or objects.)