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

158
Visualizações
Not quite understand what does the i stand for in forEach function in my code

I am using this method to find the frequency of words, however i am a bit confused with the whole code.

Here is the code:

function check(){
 var word = document.querySelector('textarea').value.split(" ");
   frequency ={};
   word.forEach(function(i){
      console.log(i)
      if(!frequency[i]){
         frequency[i] = 0;
      }
      frequency[i]+=1
   })
console.log(frequency)
}
<textarea></textarea>
<button onclick = 'check()'>check</button>

I just wonder what does the i stand for and what does the frequency[i].

Could someone explain to me a little bit because I think this code is not quite friendly for me, the beginner.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

foreach iterates over array, and as paramter has function into which is passed as parameter actual element of array. So i in this function is actual element of 'word' array.

for object frequency is frequency[i] the i'th element. At start, this object is empty, so frequency[i] will be undefined, but in foreach loop you filling this object with some values so in next iterations there may by some values

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