Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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.