Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


10,192 Points

I am trying to understand this JS code

I found this exercise in Codecademy, I am struggling to understand it flow, especially the second for loop for(var j = i; j < (myName.length + i); j++).

text = "Blah blah blah blah blah blah Eric \ blah blah blah Eric blah blah Eric blah blah \ blah blah blah blah blah Eric";

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

// Look for "E" in the text for(var i = 0; i < text.length; i++) { if (text[i] === "E") { // If we find it, add characters up to // the length of my name to the array for(var j = i; j < (myName.length + i); j++) { hits.push(text[j]); } } }

2 Answers

It is strange to find an answer about Codecademy code on Treehouse forum but if you talking about this: http://external.codecademy.com/courses/javascript-beginner-en-XEDZA/0/1

Here is something about it: http://external.codecademy.com/forum_questions/5490676fe39efe9f78001ce5

The second loop looks like this?

var text, mName, hits;
text = 'LololoDan!';
myName = 'Dani';
hits = [];

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

We make first loop just to find the first letter and if we find that first letter of the name is equal the letter i, we start internal cicle that is length = current letter and not bigger then name length + current index.

Mat Morris
Mat Morris
10,292 Points

If you could, please take a screenshot of the exercise, that will help with how you have your question setup.