Search text for name - crazy output!


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/7?curriculum_id=506324b3a7dffd00020bf661

Hi - I’ve been working on “Search Text for your Name” lesson for a while and can’t quite figure it out. At first, it would give me a long string of "E"s. I went in and changed some code based on what I saw on this forum, but now I get this weird result:
[ ‘M’,
‘y’,
’ ‘,
‘n’,
‘a’,
‘M’,
‘y’,
’ ‘,
‘n’,
‘a’,
‘M’,
‘y’,
’ ‘,
‘n’,
‘a’,
‘s’,
’ ‘,
‘t’,
‘o’,
’ ‘,
‘c’,
‘l’,
‘a’,
‘s’,
‘s’,
’ ',
‘E’,
‘m’,
‘i’,
‘l’,
‘y’ ]

Can anyone help me figure out what is wrong? Thanks!

```

/*jshint multistr:true */

var text = “My name is Emily and when Emily goes to class Emily doesn’t always pay attention.”;
var myName = “Emily”;
var hits = [];

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

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

<do not remove the three backticks above>

#2

The problem arises in your second for loop.


#3

Thanks, Dave - could you be a little more specific? I’ve changed that second for loop a dozen times and can’t get it right.


#4

your second for loops start point should be the index of your first for loop,

and just reverse the second condition so it’s like so, e < (myName.length + i)

the increment is fine, all in all

for (var e = i; e < (myName.length + i); e++) {


#5

Also inside your second for loop all you need to do is push the letters you are looping over,

hits.push(text[e]);


Hope this clear enough, if otherwise don’t hesitate to ask for clarification. :grin:


#6

This worked! Thanks so so much!


#7

Glad to hear that :slightly_smiling_face:


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.