My output is correct, why do I get an error?


#1


This is the exercise I'm getting wrong. My output is ['D','a','n'], however the error is:

Oops, try again. Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.

I search the text for myName, and 'push' it to the hits array-- where's the problem? Why you so picky Codecademy?


var text = "Hello Dan.  It's Tuesday, May 13.  The first day of the rest of your life.";
var myName = "Dan";
var hits = [];

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

console.log(hits);


#2

It could be the SCT is checking the code in the nested for loop. Did you try with the i offset in the conditional?

i + myName.length

hits.push(text[i])

Your code is correct from a working standpoint.


#3

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