Could someone help me with the second for loop 'hit' part?


#1

Could someone help me with the second for loop 'hit' part? Also I'm unable to completely understand what second loop does and what is the hit.push used for?

/*jshint multistr:true */
text="Kart I love Html,Kart I love JS,Kart Python is cool,Kart";
myName="Kart";
var hits=[];


    for(var i=0; i<=text.length; i++){
    
         if(text[i]==='K')
          {
             for(var j=i;j<=(i+myName.length); j++){
            
                      hits.push(text[j]);

                 }
        
           }

   }

#2

Remember, zero-indexing, so this should be, j < i + myName.length;.

Likewise,

Array.push() is a method that appends an element to the end of the array and sets the value that is passed in the argument.

my_array = [];
my_array.push("This is in the first element");
console.log(my_array);
// ["This is in the first element"]

Using this method we can push any object to the array, be it a primitive, a value or data structure.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.