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
Jonathan Martinez
Full Stack JavaScript Techdegree Student 6,439 PointsHardest question I've ever worked on.
So I'm working on a code right now that is supposed to show every letter of my name in the console, however, when I run it, it give me back the number 2. I can't figure out what I've been doing wrong and I've been stuck on this for a day.
var text = "Jon, Jon, Jon";
var myName = "Jon";
var hits = [];
for (var i=0; i<text.length; i++); {
if (text[i] === myName[j]) {
for (var j=i; j<(myName.length + i); j++); {
hits.push(text[j]);
}
}
}
4 Answers
Jeff Busch
19,287 PointsHi Jonathan,
I'm not sure how you are trying to print your name to the console. You would need to do something like console.log(someVariable); or console.log("some text");. But looking at your code I can see a couple of things that aren't correct. It looks like you are using arrays in your loops and conditional statements but you haven't declared arrays. You need to do this:
var text = ["Jon", "Jon", "Jon"];
var myName = ["Jon"];
var hits = [];
Also, you don't need semicolons after ifs and fors. But if all you are trying to do is find a pattern in an array you might try something like this:
var text = ["Jon", "John", "Johnny"];
var myName = "Jon";
for(var i = 0; i < text.length; i++) {
if(text[i] === myName) {
console.log(text[i]);
}
}
Jeff
Jonathan Martinez
Full Stack JavaScript Techdegree Student 6,439 PointsThanks Jeff,
What I'm trying to do is get the console to push the letters in the hits array. So I am trying to use .push to print out "J","o","n". Unfortunately it's printing out the number of letters in my name. I fixed my code a little but and now it's returning the number 3. I'm still getting my feet wet with JavaScript so forgive me if I sort of don't understand how to phrase everything.
Jonathan Martinez
Full Stack JavaScript Techdegree Student 6,439 PointsAwesome! I was able to figure it out. Thanks again for the help!
Jeff Busch
19,287 PointsGlad to hear it.
Why don't you share your solution with us? We are all learning here. If you are interested here is a neat e-book: Eloquent JavaScript and this is the zipped version to download: Eloquent JavaScript - html, zipped .