Search text for ur name,chapter6/7


#1

/*jshint multistr:true */
var text="My name is shihab,Everyone know me as shihab,\
shihab is my nickname";
var myName="shihab";
var hits=[];
for (i=0; i<=text.length; i++){
if (text[9] === "s") {
for (j = text[9]; j <= (text[9] + myName.length); j++) {
hits.push("shihab");

     }
   }

};
if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}

My name "shihab" is showing about 50 times instead of three!!! Can anyone sort it out??


#3

Compare your code to the the following model:

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