Push.hit() not working?


#1


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


Once I save&submit code all I receive is [], when it should be printing out my name.


/*jshint multistr:true */
var text = "Hello my name is Darragh \
and I really do love being Darragh as \
it isn't a very common name to have, is it?";

var myName = "Darragh";

var hits = [];

for (var i = 0; i < text.length; i++) {
    if (text[i] === "D") {
    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

if (hits.length = 0) { here you're assigning hits.length value to zero, you need to use comparison operator like == or ===, because that statement inside 'if' doesn't make any sense.


#3

Ahaha I should've spotted that. It works now, thank you very much.


#4

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