Curious why this isn't working


#1

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

My name is not printing in the console. But the tutorial says I’ve succeeded. Can someone tell me why please? Thanks!



/*jshint multistr:true */

var text = "Garrett James, Garrett James, always felt a little \
strange about the way that human beings treated the world.";

var myName = "Garrett James";
var hits = [];

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

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


#2

your loop condition:

for(var j = i; j < 0 + myName.length; j++)

why 0 here?

here:

hits.push(i)

why push i to the array?

here:

if (hits.length = 0)

a single equal sign means assign, so you give the array a length of zero instead of checking of the length equals zero


#3

Thanks for asking those questions, it helped me to identify my issues and work it out. Much appreciated.


#4

That is even better that my question identify your issues and that you could figure out the solution yourself, well done :slight_smile:


#5

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