Not Pushing name into array


#1

So this seems to be passing the requirements for Codecademy but it's an empty array each time which is frustrating. I cannot see what I'm doing wrong, a lot of people seem to put the first letter of their name in quote marks rather than myName[0] but this doesn't seem right to me. Perhaps I'm wrong, but changing myName[o] to "M" also didn't pass anything to the array. So here's my code:

var text = "So this is a string of text. It's supposed to be long and contain my name somewhere in it like pretty Mark randomly";
var myName = "Mark";
var hits = [];
for(var i = 0; i < text.length; i++) {
    if(text[i] === myName[0]) {
        for(var j = i; j < (myName.length + i); j++){
           hits.push(text[j]);          
        }
    } 
}
if(hits.length = 0){
    console.log("Your name wasn't found!")
}
else{
    console.log(hits)
}

#2

take a look at this line:

if(hits.length = 0)

see any problem? You assign a value 0 to hits.length, while you should use a compare here (two or three equal signs)


#3

Ah thank you! That was a mistake. However, the array is still empty, only this time it looks like [ ' ' ]


#4

var text = "So this is a string of text. It's supposed to be long and contain my name somewhere in it like pretty Mark randomly";
var myName = "Mark";
var hits = [];
for(var i = 0; i < text.length; i++) {
    if(text[i] === myName[0]) {
        for(var j = i; j < (myName.length + i); j++){
            console.log(text[j])
           hits.push(text[j]);          
        }
    } 
}
if(hits.length === 0){
    console.log("Your name wasn't found!")
}
else{
    console.log(hits)
}

i fixed your compare, and it worked, i added a additional console.log to see if it reach the inner loop, does this work?

for next time, use one of the two following options to make your code/indent visible:

select your code and press ctrl + shift + c (or cmd + shift + c if you use a mac)

if this instructions are unclear, you can also insert 3 backticks before and after your code, like so:

```
<p>visible</p>
```

the backtick is located above the tab key on your keyboard


#5

Ah thank you very much I managed to make it work, but for some reason the code reacts differently in different parts of the lesson.

Would you be able to explain to me why the second for loop is limited to myName.length + i like so:

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

I followed the instructions and figured it out but I don't know why it is that case.

EDIT: Also about the making the code visible? Where do you mean to put it? Before and after all the code or just the code that runs onto another line?


#6

your first for loop:

for(var i = 0; i < text.length; i++)

loops over the string stored in your variable text, the moment a hit is found:

if(text[i] === myName[0])

you want to push the hits to your array list. So in your second for loop:

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

you want this loop to run as long as j equals a hit from your myName


#7

And how does that equate to j < myName + i?


#8

That is the whole point, let's say we have the following string:

text = "Mark"

and then:

myName = "Mark"

now the first for loop will loop over the string, so then i = 0; so then in your second for loop:

var j = 0; 0 < (4 + 0); j++)

now j will increase to 1,2,3 and three which is a match with the index of your string stored in text.


#9

Ahhh so it's always going to run, given that the first one runs.


#10

Well yes, as long as your condition in your second for loop is good


#11

when j=4 so what happen ??
is it leave the second for loop ?
where is the ending of the first loop?


#12

if j = 4, then the second for loop ends, and then if there is other hit, j gets assigned a new value (of i), then loops over the the value in myName. This repeats for as long as there are hits.

The first loops ends when the entire text has been looped through


#13

text = "blahblahericblaheric"
first loop when i=8
j=8; 8<(8+4);j++
here I will push all the first eric to my array hit ? right?
it ends when j=11

then here for(var i = 0; i < text.length; i++)
so , i++ = 9 isn't it ?
second loop when i=9
j=9; 9<(9+4);j++
I'm confused here !! it will check again the first eric not the second one in my text


#14

yep, you understand it. when j = 11 it is smaller then 8+4, but when j = 12 it is not smaller then 8+4

hold on, lets index it:

 b l a h b l a h e r i  c  b  l  a  h  e r i c
 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

If you check for the first letter, i should be 16 when it encounters a hit, can i see your full code?


#15

text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";

var myName = "Eric";
var hits = [];

// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
	if (text[i] === "E") {
		// If we find it, add characters up to
		// the length of my name to the array
		for(var j = i; j < (myName.length + i); j++) {
			hits.push(text[j]);
		}
	}
}

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

#16

This will push all hits to your hits array, why doesn't this make sense? after the j for loop the i loops resumes, and j loop is going to happen again if another hit encountered


#17

Thank You very much :smile: ... I will trace it again , I lost my mind :smiley:


#18

It is tricky, but it is very good you take your time to figure it out. A hint, at some print statements so you can see what certain values are while the code is running