The code doesn't work


#1

Hi everyone!
I reach the end of the exercise but then the console prints []

var text = "My name is Giada\  Mio padre ha scelto il nome Giada\ anche se mi dovevo chiamare Elisa";
var myName = "Giada";
var hits = [];
for (var i=0; i<text.length; i++) {
    if (text[i] === myName[0]) {
        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);}

What am I doing wrong?
Thanks


#2

if (hits.length = 0) {

we use = for assignment if we want to check whether two values are equal or not we should use ==or ===


#3

Thanks, now the code works!


#4

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