6.7 / Suggested fine-tuning


I tried to tweak the program so that it finds exacts matches for the given name using this code:

var text = "blah blah Niovi blah blah blah blah Niovi”
var myName = "Niovi";

for (var i=0; i < text.length; i++) {
var lol = text.substr(i, myName.length);
if (lol === myName) {
So far so good. But then I wanted it to do something like "Your name was found x times in the text" so I used this:

console.log(lol.length / myName.length)

But it returns 0.1 because apparently lol.length === 1! Why? And how can I fix it?


@v31ns try this

var text = "blah blah Niovi blah blahN blah blah Niovi";
var myName = "Niovi";
var hits = [];
var found;

for (var i = 0; i < text.length; i++) { 
   if (text[i] === myName[0]){
        what i am doing here is that
        if you find an 'N', then look ahead
        exactly 4 characters away from your current position
        if,together with the N matches my entire name
        then push to hits
      found = text.substring(i, i+myName.length);
       //debugging statement
       console.log("Found '"+found+"'");
       if(found === myName){
            console.log("'"+found + "' is a hit . Adding it to hits...\n");    
           console.log("'"+found + "' is a not hit. Moving on...\n");

console.log(hits);//[ 'Niovi', 'Niovi' ]
console.log("There were "+ hits.length + " hits of '"+myName+"' found in the given text");