-6

In this example, what is [i] used for and what is this part of the function called? I am in a coding BootCamp and have the correct answer, but I am not sure how it actually works.

 function campgroundCapacity(campgrounds) 
 {
     let counter = 0;
     for (let i = 0 ; i < campgrounds.length ; i++)         
     {
         counter += campgrounds[i].partySize;
     }          
     return counter;
 }
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • 2
    It is accessing an array, just like in other programming languages. In this example `campgrounds` is an array so if you want to access the 3rd item in `campgrounds` you would do `campgrounds[2]` – slebetman Nov 02 '21 at 20:07
  • Accesing the position "i" in the array campgrounds, but this will also work for Objects if "i" is a string you could access the value of the Object with the key "i" – Woohaik Nov 02 '21 at 20:10
  • 1
    Did they cover how to access the indexes in an array? You might want to go over that section again. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array – epascarello Nov 02 '21 at 20:16

3 Answers3

0

Your function is expecting an array of campgrounds objects.

[i] is the "index" of a campground within the campgrounds array.

Your i could have any name. In fact, your code is deciding the name within the for loop.

for (let i = 0 ; i < campgrounds.length ; i++)
     ^^^^^

You're defining i here!

--

For instance, if you had a list of campgrounds:

const myCampgrounds = [
  {name: 'zion', partySize: 6}, 
  {name: 'bryce', partySize: 12}, 
  {name: 'yosemite', partySize: 23}];

Then the value of each object would be available at the index:

> campgrounds[0].name
<- 'zion'
> campgrounds[0].partySize
<- 6
> campgrounds[2].name
<- 'yosemite'
> campgrounds[2].partySize
<- 23

In your function, you are iterating over the array. And then you are finding the sum of all campground.partySize.

Kyle W
  • 13
  • 4
0

Loops offer a quick and easy way to do something repeatedly. A for loop repeats until a specified condition evaluates to false.

A for loop looks like this

for ([initialExpression]; [conditionExpression]; [incrementExpression]){
   statement
}

So, in your case, initialExpression will be the variable i which initial value will be 0. The conditionExpression in your case is when the variable i reaches the campgrounds array length. The incrementExpression is incrementing i in one.

When a for loop executes, the following steps occures:

  1. The initializing expression initialExpression, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity. This expression can also declare variables.

  2. The conditionExpression expression is evaluated. If the value of conditionExpression is true, the loop statements execute. If the value of condition is false, the for loop terminates. (If the condition expression is omitted entirely, the condition is assumed to be true.)

  3. The statement executes. To execute multiple statements, use a block statement ({ ... }) to group those statements.

  4. If present, the update expression incrementExpression is executed.

  5. Control returns to Step 2.

Read more about loops here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for_statement

Ran Turner
  • 14,906
  • 5
  • 47
  • 53
0

In simple i is index (from iterator), in the for loop it means the current element that is 0, 1, 2, 3 for every iteration, Eg: printing all element in an array we can use [i] as the current element and console.log(array[i]) the current element.

Sudarshan
  • 702
  • 6
  • 24
Evil-Coder
  • 11
  • 1
  • 4