Code is same as initial example but doesn't function correctly


#1

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

So the code runs and I don't get any error messages but my name doesn't print out, it finds the a in rainbow and correspondingly prints out "ainbo but" then all following "a"s are ignored and not printed. Would just like to know why this happens as I checked back and I think the code is the same as the example at the start which didn't stop at the first "E" it found.
Thanks

Replace this line with your code. 

var text = "rainbows and butterflies unicorns \
called alice fairies with wands magical castles"
var myName = "alice"
var hits = []
for (var i = 0; i < text.length; i++) {
if (text[i] === "a") {
    for (var j = i; j < (myName.length + 1); j++) {
        hits.push(text[j]);
    }
}
}
if (hits.length === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log(hits);
}


#2

Left my comments above.I hope I cleared some things up. Try changing the myName variable to 'Alice' and the alice in the text to 'Alice' as a test.


#3

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