Search Text Second Loop. Need explanation


#1


var text = "Node.js is a platform built on Chrome's JavaScript \
runtime." 
var myName = "Atamyrat";
var hits = [];

for (i=0; i<text.length; i++ ) {
    if (text[i]=== "e")
{
 for (var j=i; j<i + myName.length; j++) 
 {
 console.log(hits.push(text[i]));
     }
        }
        
    
    }


#2

First of all it would make sense if your name (the one stored in myName) even occurs once in the text. Likewise the letter you search for here:

if (text[i]=== "e")

should be the first letter of your name. And finally think about what you're doing here:

 for (var j=i; j<i + myName.length; j++) 
 {
 console.log(hits.push(text[i]));
     }

First of all i stays the same this entire loop so you're basically adding the same letter over and over again to the array. And you probably don't even see the letter printed by the console.log but rather a number indicating the new length of the array, are you aware of this? If not you might google MDN + push to see what this function does.

Is this answer sufficient? Otherwise maybe add some more context to your question.
Good Luck!


#3

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