Code accepted but name is not printed


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/7?curriculum_id=506324b3a7dffd00020bf661

Hi, I've been stuck at this for almost 2 days since I'm lazying eversince the start of this array/loop exercise. I don't want to move forward without fully understanding these two elements.

So, I have compared my code with a lot here on the forum but cannot pinpoint my error. There was this advise to remove ( ; ) at some parts of the code and I did that but still the only thing printed on the console is []. Does this mean my name's characters were not pushed into the array?

Appreciate your help, please. Million thanks!


text="Blah blah blah blah blah Anj"
var myName="Anj";
var hits=[];
for (var i=0; i<text.length;i++) 
{
    if(text[i]==="A")
    {
        for(var j=i; j < i + myName.length;j++)  
        { 
            hits.push(text[j]);
        }
    }
}
if (hits.length = 0) {
    console.log("Your name wasn't found");
}
else {
    console.log(hits);
}


#2

= is the assignment operator, you cannot use it to test length, it will not do that.

I suggest adding more prints to find out what's being done or not done. For example, you can add prints which show the array before and after pushing to it.


#3

Stupid of me, I completely forgot about the === thing. It's working fine now. Thanks a lot!! :slight_smile:


#4

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