Says all my code is correct, but it is not returning my name. Please help!


#1



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


It says that I have completed the course, but when I save and submit I get [ 'l', 'a', 'h', undefined, undefined, undefined ]


var text = "blah blah blah Dan blah blah blah Dan blah blah blah"
var myName = "Dan"
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[i+j])
        };
    };
}
if (hits === 0) {
    console.log("Your name wasn't found!");
}
else {
    console.log(hits);
}


#2

Hello desco5,

The reason, I believe, that you aren't getting the proper result is because of the following line of code
hits.push(text[i+j])
Since you have already initialised j to be i, you just need to say
hits.push(text[j])
Hope this solves your problem

Regards,
Barry Allen


#3

Thank you! I changed it and it worked!


#4

You are truly welcome...

Regards,
Barry Allen


#5

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