6/10 Log it Help!


#1

Hi,

I need help with my codes, manage to pass this level but the console log does not seems to print out anything.

/*jshint multistr:true */
var text = "How are you? My name is Ken. I friends call me Kenny instead of Ken sometimes.";
var myName = "Ken" ;
var hits = [] ;
for ( var i = 0; i < text.length; i++ ) {
if (text[i] === "K") {
for (var j = i; j < myName.length; j++){
hits.push(text[j]);
}
}
}

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


#2

The problem with your code is here

j < myName.length

As you can see j already equals i. In your example the first mention of myName in text is at position 24.

So from here :

var j = i;

which means j = 24 which is already bigger than the length of the myName variable and with other words that stops your for loop and nothing gets pushed into the hits array.

To make it work you should add:

j < myName.length + i

this way the length of j wont be bigger than the length of myName and it will push the rest of the letters into the hits array.

Sorry if my explanation was not very clear...English is not my native language and it is not easy for me to explain anything in it :smile:


#3

Hi,

Thanks for your explanation! I understand the first part but the second part below,

Could you please clarify the code above which means that the code will start at 24 and +I would mean it would add on the balance of the word since j=i.

Hope to hear from you soon.