5/7, something's wrong, but what?


#1

please help! I can't seem to find what I'm doing wrong

var text = "leggo my eggo";
var myName = "☀ morgan ☀ ";
var hits = [];
for(var i=0; i if (text[i] === 'E'){
for(var j=i ; j <(myName.length + 1) ; j++)
{
hits.push(text[l]);

}
}}


#2

You need to make if inside your second for loop that will register hits. Also this for(var i=0; i if (text[i] === 'E') doesn't mean anything. You can't write it like that. Try to fix your code, if you don't know how come again and I'll supply you with correct one.


#3

the post kind of messed up my code, could you please post the correct code? javascript's been a bit difficult for me man, thank you!

for(var i=0; i < text.length; i++){
if (text[i] === 'E'){
for(var j=i ; j <(myName.length + 1) ; j++)
{
hits.push(text[l]);

}
}}


#4

var text = "Hey, how are you doing? My name is Igor. Igor Igor. Igor is my name, yes";
var myName = "Igor";
var hits = [];

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

I don't have much time to write an explanation now, but if you need one let me know what parts of solution you don't understand and I'll explain it to you.


#5

That's a pretty cheeky way to write the test value in the first if statement instead of "I" , I like it :slightly_smiling:


#6

Well I expected people to use their own names, and wouldn't want the code to fail for them. :slightly_smiling:


#7

If (text[i] === "T") would this line work instead of if (text[i] === myName[0]) asuming my name is stated as Tom ?

What got me was for(j = i; j < (myName.length + i); j++) because i did it like this :
for (var j = i; j < myName.length; j++) . Could i ask you or somebody to explain why this should be like it is ?


#8

It would work, because myName[0] would be T in your case. In mine, it was I etc. It will take first character (first is with index 0) so you can write whatever you want inside myName.

Now, you don't understand the second for loop, right? If so...
What we did there is said that j = i, meaning our starting index inside text variable will be the same as as index where first letter of our name was found. Then we proceed to add characters from our name to our hits variable. Why? Because we got until j < (myName.length + i). Why we had to add i? Here is example (on your case)!

// code
var myName = "Tom";
for (j = i; j < (myName.length + i); j++) {
  // code
}
// code

Now lets say capital T was found at i = 10 first time. We would say j = 10 and go until j < 3. That would obviously fail. But we notice that we don't start from j = 0, but rather from index 10. That's why we add 10 (in this case, otherwise variable i to our stopping condition in second for loop).
Your for loop failed because it instantly met stopping condition. Sure, it would have worked if you had something like var text = "Tom, my name is.";. Otherwise, it would never capture your whole name. But if you raise the length to length + starting index...different story. It makes sense when you go through it, try to write it on paper and figure it out, you'll see it's logical.

I hope this helped a bit, if you need more examples or explaining let me know.


#9

This cleared it up thank you so much :slightly_smiling: