Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda