Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
What is [i] used for in a javascript function?

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;
 }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

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.

about 4 years ago · Juan Pablo Isaza Report

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

about 4 years ago · Juan Pablo Isaza Report

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!