-2
var text = "Hello world Keya how you doing Keya";

var myName ="Keya";
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]);
    }
  }
} 

I don't understand what is hits.push

secondly, I don't know

for (var j=i; j< i+ myName.length; j++)
Uttam Kumar Roy
  • 2,060
  • 4
  • 23
  • 29
kkab
  • 1
  • 1
  • You tried googling these things? Some search queries: `javascript push`, `javascript for loop`, `javascript arrays` – Wouter J Jan 23 '16 at 11:42
  • 2
    So really what you're saying is that you don't understand javascript, and should get a book ? – adeneo Jan 23 '16 at 11:42
  • Possible duplicate of http://stackoverflow.com/questions/34951310/what-does-re-assigning-a-variable-in-a-for-loop-in-js-mean/34955348#34955348 – guysigner Jan 23 '16 at 11:45
  • I don't understand what is hits.push – kkab Jan 23 '16 at 11:48
  • Possible duplicate of [Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons) – J.Tural Jan 23 '16 at 11:53
  • please move the `if` block inside the first `for` block. – Nina Scholz Jan 23 '16 at 12:01
  • for `push`, try Googling "MDN push". MDN is a great site which will answer many of these questions for you. For the `for` statement, you could google "MDN for". –  Jan 23 '16 at 14:46
  • To close voters: this question may not be very useful, which means you can choose to downvote it if you so desire. However, it is not too broad, or otherwise off-topic. Please follow the reasons given for close votes; they're there for a reason. –  Jan 23 '16 at 14:47

2 Answers2

0

This code for the myName variable which is in this case 'Keya' in the string text .

hits variable store the name as an array.

  • for(var i=0; i<text.length; i++ ) Run through the string
  • if (text[i]=== 'K') If text[i] is the first letter of your name (it would be more flexible if you change it to name[0])
  • for (var j=i; j< i+ myName.length; j++) Run through the string starting for from i (the index the first letter in the name found in text) to i+ the length of the name

  • hits.push(text [j]) Add text[j] to the array ( see JavaScript Array push() Method )

Marox Tn
  • 142
  • 9
0
   var text = "vinod kumar";
    var myName = text;
    hits = []

    for(i=0; i < text.length; i++) {
        if(text[i] == "u") {
            for(var j=i; j < (myName.length+i); j++ ) {
                hits.push(text[j]);
            }
        }
    }
Vinod Kumar
  • 1,191
  • 14
  • 12