There is no nested arrays. You are just nesting two array access syntaxes.
To explain this code, we first need to know how will the answer
variable change. From the for loop header, we can see that it starts from 0, and goes all the way up to responses.length - 1
, which is 8. Now we can evaluate the expression frequency[responses[answer]]
:
// in each iteration of the loop
frequency[responses[0]]
frequency[responses[1]]
frequency[responses[2]]
frequency[responses[3]]
frequency[responses[4]]
frequency[responses[5]]
frequency[responses[6]]
frequency[responses[7]]
Now we can evaluate the responses[x]
part. We just need to find the corresponding response in the responses
array. responses[0]
is the first item, which is 1
.
frequency[1]
frequency[2]
frequency[3]
frequency[4]
frequency[4]
frequency[4]
frequency[4]
frequency[4]
The statement also includes the ++
operator, which increments that particular index of frequency
by 1. So all of the above indices will be incremented by 1 one after another, making the frequency
array look like this:
[0, 1, 1, 1, 5, 0]
On a higher level of abstraction, this code is counting how many times a particular response appears in the responses
array. For example, 4
appeared 5 times.