3.7 Passed but - Returns too many of my name


#1

Hi all,

I'm trying to figure out why my name shows up so many times in the array. Is it because I used i++ and j++? What else would I have used instead?

Thanks!

Code:
/*jshint multistr:true */

var text = "May likes to climb, May likes to ski\
May likes to dance...and other stuff too";

var myName = "May";

var hits = [];

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

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

Result:

[ 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May' ]


#2

Hi try to change in hits.push("May"); May by text[j] like that hits.push(text[j]);


#3

Thank you that was it!