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

JavaScript

Javascript program found on the net help?

Hey guys, so I am looking to build on what i learned on treehouse, especially with javascript as somehow these videos have been the toughest ones. I completed the program below by going to codeacademy (not sure if its kosher to ask about this stuff here if not let me know). My question is, on my second "for" loop I had to add a (J < myName.length + i) in order to get the loop running correctly...why did I need to add a + 'i"?

See the complete code below:

var text = "Hey, my name is Alcibiades \ I am quite happy to see you\ Because my name is Alcibiades I shall be here for you";

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

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

if (hits.length === 0) { console.log("Your name wasn't found!"); } else { console.log(hits); }

4 Answers

Umm... I haven't done the code academy drill that you speak of.(but i will :) ). This is an issue regarding how your loops are working. Your "var i" is at index zero. As you traverse your sentence, you get to "text[17]" index, correct? Your "if" statement evaluates to "true". You enter your loop. The "var j" gets assigned to "i" which is 17. Your name length is 10. Your condition is that "j is less than myName.length" which would be false if you didn't add the "i". You would need to add the distance that you traversed through the sentence plus the length of "myName" to satisfy your condition.

PAUL that was SUPER HELPFUL:

Specifically this: "You would need to add the distance that you traversed through the sentence [i] plus the length of "myName" (myName.length) to satisfy your condition. It will take a couple of times to analyze loops the way you did but I am guessing I have to start doing that in order to be able to build them!

P.S: Recommend going through the Javequery + Javascript sessions of Codeacademy they supplemented the stuff I learned here and because I saw the videos here first I have been flying through the lessons while feeling like I am learning.

PAUL that was SUPER HELPFUL:

Specifically this: "You would need to add the distance that you traversed through the sentence [i] plus the length of "myName" (myName.length) to satisfy your condition. It will take a couple of times to analyze loops the way you did but I am guessing I have to start doing that in order to be able to build them!

P.S: Recommend going through the Javequery + Javascript sessions of Codeacademy they supplemented the stuff I learned here and because I saw the videos here first I have been flying through the lessons while feeling like I am learning.

Maybe there is a better way. But just explaining how the codeacademy drill looks to me. Cool, man. I definitely will! Keep me posted :)