Nothing happens, but the site accepts my code


#1


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


it doesn't search my name - "Slava", yet the site accepts my code as correct. Where is mistake?


/*jshint multistr:true */
text = "bla bla bla bla bla bla Slava bla bla bla bla bla bla bla bla \
bla Slava bla bla bla bla bla bla bla bla bla bla bla bla bla bla \
";
var myName = "Slava";
var hits = [];
for(var i=0; i<text.length; i++){
    if(text[i]==="S"){
        for(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

you assign the length of you array a zero of value, you should check if the length of the hits array equals zero


#3

hits.length=0 should be hits.length==0

You're not actually checking for anything in your if statement. Instead, you're assigning the length to 0. You need the double equals.


#4

Thanks! Now everything works properly. But still hard to get a handle on the topic of loops and arrows. Could write a simple code with them without the site's tips, not to speak of improving this name searching code. Any advice?


#5

of course you can use regular expression search, or split to split the string into array. That make things a lot easier, but you also need to learn to build algorithms.


#6

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