Second 'for' loop isn't pushing values to the hits array


#1

Hi, just hoping someone can explain my problem:

/*jshint multistr:true */
var text = "here is a string of text";
var myName = "Claire";
var hits = [];

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

I get the response : It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.

Any help appreciated!


#2

Hi here

var text = "here is a string of text";

You should put at least one time your name inside the variable text.


#3

var text = "Claire here is a string of text";
var myName = "Claire";
var hits = [];

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


#4

This is my code and there seems to be a little problem;

var text = "Blah blah Steve blah blahblah blah Steve,blah blah blah Steve blah blah blah Stan ";
var myName = "Steve";
var hits = [];

for (var i = 0; i < text.length; i++){
if (text[i] === myName[0]){
for (var j = i; j < (i + myName.length); j++){
hits.push(text[j]);
}
}
}
if (hits.length === 0)
{
console.log("Your name wasn't found!");
}
else
{
console.log(hits);
}

it returns my name,but also every word starting with a capital S which is 5 or less letters long...
How to search an entire word in a text?