How do I end the second For Loop?


#1


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


/*jshint multistr:true */
var myName = "Josiah";
var text = "Hey, how are you \
doing? My name is Josiah.";
var hits = [1];
for(var i = 0; i < text.length; i++); {
 console.log ("Hey, how are you doing? My name is Josiah.");
};
if (text[0] === "J") {
    for(var j = i; j < myName.length; j++); {
        newArray = [];
        newArray.push("Hey,");
        newArray[0];
    console.log ("J");
    };
};
console.log(hits);


#2

What is wrong this time.... :confused:


#3

The second loop ends :
for(var j = i; j < (i + myName.length); j++){

        hits.push(text[j]);


    }

#4

thanks for the help!


#5

the problem now is the letters won't push the hits array... Here is the new code

/*jshint multistr:true */
var myName = "Josiah";
var text = "Hey, how are you \
doing? My name is Josiah.";
var hits = [6];
for(var i = 0; i < text.length; i++ ); {
console.log ("Hey, how are you doing? My name is Josiah");
};
if (text[0] === "Josiah") {
for(var j = i; j < (i + myName.length); j++); {
newArray = [];
newArray.push('Hey');
newArray[0];
hits.push(text[j]);
}
};
console.log(hits);


#6

Take a look at my comments:

/*jshint multistr:true */
var myName = "Josiah";
var text = "Hey, how are you \
doing? My name is Josiah.";
var hits = [6]; // You should declare an empty array. var hits = [];
for(var i = 0; i < text.length; i++ ); {
console.log ("Hey, how are you doing? My name is Josiah"); //That's not necessary to the program
};
if (text[0] === "Josiah") { //First you have to look for the first letter of your name instead of your whole name. Second you have to specify the array's position you want to look. if (text[i] === "J")
for(var j = i; j < (i + myName.length); j++); {
newArray = []; //This is not doing anything
newArray.push('Hey'); //This is not doing anything
newArray[0]; //This is not doing anything
hits.push(text[j]);
}
};
console.log(hits);

Try now so we can see if it works.


#7

this is what i'm getting when I do that
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

It will only push values to the array if the result of your comparison is true. In your code this will never happen because you are comparing text[0] with "Josiah" and text[0], in your case, is always "H".


#9

ok... I'll try that.


#10

Unfortunately, it didn't work for me..... :disappointed:


#11

here is my new code...

/*jshint multistr:true */
var myName = "Josiah";
var text = "Hey, how are you \
doing? My name is Josiah.";
var hits = [];
for(var i = 0; i < text.length; i++); {
console.log ("Hey, how are you doing? My name is Josiah.");
};
if (text[0] === "h") {
for(var j = i; j < myName.length; j++); {
newArray = [];
newArray.push("Hey,");
newArray[0];
console.log ("h");
};
};
console.log(hits);

here is my outcome

Hey, how are you doing? My name is Josiah.
[]


#14

Here you can find explanation and how to fix your errors.


#15

Thanks @denys.matsevych


#16

I've been stuck on this part for like the last month or so this is really annoying me... And I still haven't passed what's wrong now? :confused: :confused: :cry: :cry:


#17

/*jshint multistr:true */
var myName = "Josiah";
var text = "Hey, how are you \
doing? My name is Josiah.";
var hits = [];
for(var i = 0; i < text.length; i++); {
};
if (text[0] === "h") {
for(var j = i; j < i + myName.length; j++); {
newArray = [];
newArray.push("Hey,");
newArray [0];
console.log ("h");
};
};
console.log(hits);


#18

And here's what I keep getting...

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.


#19

Did you read the post? You can find how to write second for loop there. So, please, read that post and review your second for loop.


#20

ok I'l try to understand it


#21

It worked Thanks!!!!!!!! :grin:


#22

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