Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

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

or

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.)