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 Object-Oriented JavaScript (2015) Practice Project Quiz Prototype

Volodymyr Boretskyi
Volodymyr Boretskyi
9,094 Points

explain please line 15 in quiz.js

what's in square brackets?

return this.questions[this.currentQuestionIndex];

3 Answers

6,387 Points

This 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
Christopher Stöckl
19,795 Points

Hi 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".


return Quiz.questions[1];

I hope I explained it good enough :)

Volodymyr Boretskyi
Volodymyr Boretskyi
9,094 Points

Yes , thanks. I have hard times with all these substitutions when looping trough arrays or objects.)