Pushes character after my name, but not my name?


#1


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


I can't figure out why my code is pushing the character after my name, rather than my name. I'm getting a "Way to go!" every time, so no error message to help me out... I tested it by changing the characters after my name -- with this iteration of the code, I got [ '?', 'B', 'Q' ]... help?


/*jshint multistr:true */

var text = "blah blah blah Adrienne? blah blah blah blah two AdrienneB blah blah three AdrienneQ.";
var myName = "Adrienne";
var hits = [];

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

I like to format my code like this.

for (var i = 0; i < text.length; i++)
{
if (text[i] === "A")
{
for (var j = i; j < (i + myName.length); j++); //<---no semicolon please.
//Squiggly
hits.push(text[j]);
//another squiggly bracket.
}
}

This way I can more easily line up and identify where I'm missing squiggly brackets and stuff.

You've got a ; where there should be none, and are lacking a pair of { }'s where there should be some.


#3

Thank you! I will pay more attention to those squigglies and semicolons in future! :slight_smile:


#4

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