5. Your second "for" loop


#1

I passed the lesson with this code:

var text = "Hey,Emily how are you \
doing? My name is Emily.";
var myName = "Emily";
var hits = [];
for (i=0;i<text.length;i++) {
for (var j = i; j < i+ myName.length; j++)
{
hits.push(text[j]);
}
}

But:
1-what is "J",
2-what do I need "hits" for?
3-the result in the console is "230".I passed with the number 230.230 of what?

after that in "Log it part" I've got some random letters...whole bunch of them.Can somebody explain pls?


#2

j is a variable (you could haven named it anything) which loops over your name (from i till i + myName.length)

it is an array which you declared here:

var hits = [];

you use it to store the letters of your name:

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

adding console.log, you can see what is being pushed into your hits array.

there are 230 letters in your hits array.

that is a bit much, don't you think? where did you if condition go which you build in exercise 4?


#3

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