Lesson 5: I got it right but I don't know why?


#1

Code:
var text = "Blah blah blah blah blah Bailey blah blah blah Bailey";
var myName = "Bailey";
var hits = []
for (var i = 0; i < text.length; i++){

if (text[i] === "B") {
for (var j = i; j < myName.length + i; j++) {

    hits.push(text[j]);    
        }
    }

}

I got 18 hits but I'm not sure why?? Shouldn't it be only 12?


#2

I think it's because the first "Blah" is capitalized. Your if statement says "if you find "B" add it to hits[]. add the next letter too, and the next one and the next one... continue doing this until you reached the length of myName it doens't tell the computer that you're only looking for "B" "a" "i" "l" "e" and "y". "Bailey" six letters so it added "Blah b" to hits [] codecademy explains it in the next lesson :slightly_smiling:

hope i was able to help you somehow :slightly_smiling:


#3

Thanks! that was it :slightly_smiling:


#4

/*jshint multistr:true*/

var text = "Hey, how are you\
doing? My name is Erika."

var myName = "Erika";

var hits = []

for(var i = 0; i < text.length; i++) {

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

}

}

Thanks your post helped me get it right, I had a semi-colon in the wrong place.