Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

54
Views
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.

7 months ago · Juan Pablo Isaza
1 answers
Answer question

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

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs