Second loop 5/7 Please someone help me!


#1

/*jshint multistr:true */
var text = "Hey darling How was\
your day?";
var myName = "Ricky";
var hits = [];
for (var i = 0; i < text.length; i++){
if (text[i] === myName[i]){
for(var j = i; j < i + myName.length; j++){
hits.push(text[j]);
}
}
}


#2

Try to compare statment if with the first letter in myName:

if (text[i] === 'R') {

And type in the very end:

console.log(hits);

It should work


#3

If you call as:

console.log(hits);

it will give you:

Happy New Year 'R', 'i', 'c', 'k', 'y' !

That is already good, but

you can try also to call myName in more clear way with orders of hits:

console.log("Happy New Year" + " " + hits[0] + hits[1] + hits[2] + hits[3] + hits[4] + "!");

That will type myName clear:

Happy New Year Ricky!

,


#4

Call me ignorant, but the reason you get no hits is because your name is never actually said in the text. In order to find your name, the text has to contain your name. Otherwise your just searching for the amount of times "R", "i", "c", "k" and "y" shows up in the text. But won't find your name because an "i" never follows an uppercase "R" and a "c" and "k" are never even in the text.

At least, that is the way that I understand how this works.


#5

@o_k_s_a_n_a @eagernoobcoder Thank you! For y'all's help I replace the variable myName with "R", console.log it and add my name into the text and it ran like a champ. :smile: