Your second "for" loop: Where's the Mistake?


#1

Hello, I need help on the Codecademy Lesson: "Search Text For Your Name". The way the lesson tells me to solve it confusingly. I read the description very carefully and smoothly, but it seems I have done something wrong.

Error Messages:

(This error message appears in the one below) Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.

(This error message appears when I change "j < (i + myName.length)" into "j < i") Oops, try again. Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.


var text = "Well, hello. How are you today"

var myName = "censored"

var hits = []

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

Please answer my post. Thank you for reading :slight_smile:


#2

Instead of trying to create a new array try using hits instead. Should come out to be something like this:

hits.something(what[what]);

Second, whatever letter you put in your text[i] also has to be what you are pushing and using in your for statement. So instead of N use J!

Hopefully this helps you fix your code and learn from it all! :smiley:


#3

Aw, yes, that is! Thank you, @bibleman13. You are a great hero!


#4

Well thank you! And I am glad it worked and happy coding! :smiley: cape swooshes in the air flies off into the sunset super bibleman theme music starts playing xD


#5

I believe that I just snorted


#6

Well what can I say? I like making people laugh! haha....or well snort xD


#7

I'm having difficulty making any sense of this. "use J"? That above statement is perfectly valid and would work fine if, a) the variable myName contained a name that started with 'N', and, b) there was actually a name in the text to match.

Even with no errors the SCT would still kick this back since the hits array will be empty.

The only place we need to use j is in the push argument: hits.push(text[j]);

For the OP, may I recommend starting with a large block of text similar to the example in lesson 1. The comment at the top must be there for the continuation operator, \ to work. Be sure to plant the name in the text at least once.

Also, if you wish to keep you own name secret, then use an alias, and treat it like a proper name, capitalized.

var myName = "Billy";

Assuming the name is found at least once (the SCT will insist upon it) then the hits array should look like,

['B', 'i', 'l', 'l', 'y']

#8

Well, I thought it would work too. In fact, when I put in the code and got errors I was a little skeptical. I had actually wondered about this situation but when I had try different combinations I never got it to work without errors. When I got the errors from this person's code and compared it to my code that I had worked on I made changes to the code based on the one I had done. Maybe more research or asking was needed. Anyhow, to fix the errors given all the steps I suggested changes in my above statement were the ones I did to fix the code. This may be where my coding help errors in the fact that I tend to lean more towards the code I put in lesson rather than try to find the answer specific to that situation in the code. However, I know that the things I am saying are correct and sure when I am working in the correct codes so I feel safer and more confident in answering because I obviously do not want to cause more problems than I am solving.


#9

To help others we should stick as close to the actual instructions as possible (as should they). Code that passes may not be what is instructed, exactly, which can add to confusion. I recommend to others that for the best results, stay with the instructions (and follow examples, when given) to pass the lesson, then play around with the code all they want (keeping their original solution in a comment, for future reference).


#10

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