I do not understand this! PLEASE HELP!


#1


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


I just need help understanding some of this stuff. Firstly, I dont understand the end part of the second for loop. I also dont understand the body of the second for loop. If someone could please help me, that would be great. I have put a comment at every part I am stuck at.

Thank you!


/*jshint multistr:true */

var text = "Hello. How are you doing today? \
my name is Ali.";

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

for (i=0; i === text.length; i++) {
    if (text[i] === "A") {
    for (j = i; /* Stuck Here*/ + myName.length) {
        //Stuck here
    }
}


#2

Hello @boardslayer54737,

You want to have j iterate over the length of your name, starting with i. So, j must be less than the length of your name, plus the variable i. Inside of your second for loop, you must .push() to the hits array the index that j is at, text[j]. Hope this helps your understanding...


#3

Is this right so far?

/*jshint multistr:true */

var text = "Hello. How are you doing today? \
my name is Ali.";

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

for (i=0; i === text.length; i++) {
if (text[i] === "A") {
for (j = i; j < myName.length; // What do I put here?) {
//Stuck here
}
}

Im still stuck in the body of the for loop. I still don't understand why I do that. Thanks for helping


#4

In the remaining part of your for loop. You have to put the condition for j to Update, or j++


#5

What is wrong with this code?

/*jshint multistr:true */

var text = "Hello. How are you doing today? \
my name is Ali Allam.";

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

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

It keeps giving me this message:

"Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable."

Thanks for your help


#6

This part is also incorrect because you need to check if j less than myName's length + i which woud look like,

j < (myName.length + i);

And lastly in this statement,

You are supposed to be pushing the text in the j loop meaning,

(text[j]);


#7

I made those changes and my code looks like this:

/*jshint multistr:true */

var text = "Hello. How are you doing today? \
my name is Ali Allam.";

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

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

But im still getting this error message:

"Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable."


#8

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