Code behaving differently but still counted as correct


#1

My code is not behaving the way the example code does, but it is still letting me continue on. It also prints my name way more than my name is found in the text.

/*jshint multistr:true */
var text = "Lorem ipsum dolor sit amet, \ consectetur adipiscing elit, sed do eiusmod tempor \ incididunt Donella ut labore et dolore \ magna aliqua. Ut enim ad minim veniam, \ quis nostrud exercitation ullamco \ laboris nisi ut aliquip ex \ ea commodo Donella consequat. \ Duis aute irure dolor in reprehenderit \ in voluptate Donella velit esse \ cillum dolore Donella eu fugiat \ nulla pariatur. Excepteur sint \ occaecat Donella cupidatat non \ proident, sunt in culpa \ qui officia deserunt mollit \ anim id est laborum.";

var myName = "Donella";

var hits = []

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

if (hits.length === 0){
    console.log("Your name wasn't found!");
} else {
    console.log(hits);
}

#2

The code was accepted because the editor has some inaccuracies, but it's good enough for use.

As far as why it prints your name multiple times, that I am unsure of.


#3

 for(var j = i; j < i + myName.length; j++){
            hits.push("Donella");
        };

you are pushing Donella in array

you should push text[j]


#4

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