Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda