-6

This below is my for-loop and im a beginner so i need help to understand what [i] means where it says ".checked" and ".value". Also, does "var i" stand for "variable i"? Side note: this loop is for a list of checkboxes.

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

if (flightprices.length[i].checked) {

flightPrice = +flightprices[i].value;
}}
epascarello
  • 204,599
  • 20
  • 195
  • 236
Sjan
  • 3
  • 1

1 Answers1

1

Ignoring the fact that this code results in an error, I can tell you the following.

  • var i=0; is defining a variable named i to the value 0.
  • flightprices[i] means the i+1th element of the array flightprices. [] notation is an array selector. Because JavaScript is 0-indexed, flightprices[0] means the first element, flightprices[1] is the second element, and so on.

Why there is an error

This line:

if (flightprices.length[i].checked) {

tries to find element i of flightprices.length, however the length of something is always an integer, and integers aren't arrays.

Community
  • 1
  • 1
ericw31415
  • 415
  • 6
  • 17