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 1 Solution

Nenad Marinkovic
Nenad Marinkovic
13,310 Points

Can someone explain me this [i] in foor loop?

I am not sure I understand this [i] in questions[i][0] ? Can someone just explain why we use that?

for (var i = 0; i < questions.length; i += 1) {

question = questions[i][0];

answer = questions[i][1];

response = prompt(question);

2 Answers

andren
andren
28,558 Points

I can try to break down the code line by line:

for (var i = 0; i < questions.length; i += 1) {

This starts a for loop that creates a variable called i and increases it by 1 each time the loop runs. The loop will end when the value of i stops being less than the length of the questions array.

question = questions[i][0];

In this line you are using that variable I mentioned above, so the value of i will depend on what iteration of the loop we are in. The first loop it will be 0, the second loop it will be 1 and so on. Let's assume this is the first iteration, in that case the code will essentially be treated like this:

question = questions[0][0];

When you place a bracket and a number after an array you tell JavaScript to pull out an item from the array at that index. So questions[0] for example tells JavaScript to pull out the item at index 0 (the first item) from the array.

Since the questions array actually contains a nested array inside it the result of questions[0] is another array, which means that you can again put brackets after it to pull out an item. So questions[0][0] tells JavaScript to pull out the first item from questions (which is a nested array) and then to pull out the first item of that nested array.

If for example questions looked like this:

questions = [["item in nested array", "item 2 in nested array"]]

Then questions[0] would pull out:

["item in nested array", "item 2 in nested array"]

And questions[0][0] would pull out:

"item in nested array"

So the line essentially just pulls out the array nested in questions, then pulls out the first item in that nested array and then assigns it to a variable called question.

answer = questions[i][1];

This line does pretty much the same thing, expect it pulls out the second item from the nested array rather than the first and then assigns it to a variable called answer

response = prompt(question);

And then this line simply opens up a prompt that displays the text that was extracted from the first item of the nested array. And then stores the answer the user gives into a variable called response.

This helped me alot too thanks! I didnt understand that too.

Nenad Marinkovic
Nenad Marinkovic
13,310 Points

Thanks a lot, Andren, for your quick and really helpful answer! I finally understand.