Question 5/7


#1

Hi, I put in this code, and to me it looks correct, but it returned an error: "Careful: your second loop should stop once it reaches its current point in the string + myName.length."

Could someone please check over it and see if you can figure out the problem? Thanks, in advance.

var text = "My name is Yemi. Yemi is a really cool name. I bet you wish your name was Yemi as well, dont you?";

var myName = "Yemi";
var hits = [];
for (var i = 0; i < text.length; i ++) {
    if (text[i] === "Y") {
        for (var j = i; j = i < (myName.length); j++) {
            hits[0];
        }
    }
}
console.log(hits);

#2

there are few problems, first one:

hits[0];

this doesn't push anything to hits. You need to use .push. anyway, the error is caused because your second loop is incorrect, let me explain. the moment this if statement is true for the first time:

if (text[i] === "Y")

i is 12 (the index at which "Y" is located). so then in your second loop, you give j a value of 12 (j = i), then i am confused by this bit:

j = i < (myName.length)

what are you trying to do?
You should do:

j = (myName.length + i)

why? your name.length is 4, so 12 (i) + 4 is 16, now j will loop till it pushed all the hits to your hits array, while i doesn't increase, brilliant isn't it?


#3

Alright so there's two things that need to be fixed.

  1. The parenthesis in your second for loop are a little off, it should be j < ( i + myName.length) ;

  2. hits[0] will doesn't do anything productive,
    So what you'll need to do is change the 0 to "j" since that's what's in the for loop, and change hits, to hits.push

So it'll look something like this when you've finished:

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

I know this is a little backwords from how you have it, but I hope the explanation was well enough!


#4

Yes @stetim94 I will run this code and clarify with you if it works. Thanks
:smile:


#5

You succeeded with my and lolman help? You also understand why it works? That is the most important bit


#6

@stetim94 I changed the code and this time, I got the same error.

var text = "My name is Yemi. Yemi is a really cool name. I bet you wish your name was Yemi as well, dont you?";
var myName = "Yemi";
var hits = [];
for (var i = 0; i < text.length; i ++) {
    if (text[i] === "Y") {
        for (var j = i; j < (myName.length + i); j++) {
            hits.push[j];
        }
    }
}
console.log(hits);

If you find the problem, please could your explanation be a little bit clearer, I am still a beginner at coding, and I sometimes sit for hours on end trying to figure out what others would consider simplistic...:pensive: :sleepy:

Thank you in advance.


#7

Now it's very likely that I've made a mistake with the grammar here, but try this:

hits[j].push;

I can't pull up the lesson and verify just now, but hopefully that solves your problem!


#8

Do you understand why your second for loop works?
now, push has brackets (.push()), so it becomes:

hits.push([j]);

but what is j? j is a number, not a letter how can we solve this? by doing:

hits.push(text[j])

text is the variable you declared on line 1, remember we can access letters from a string by using the index position? for example, console.log(text[0]) gives you the first letter of a string.


#9

Di had a brain fart, thank you @stetim94