5/7 I have no clue what I'm doing wrong


#1

I seem to be doing something wrong, because everytime I run the code it crashes. That would lead me to believe that I am not stating the end of a ''for'' loop correctly. Yet I still can't figure out what it is. Could anyone please help me?
Any help would be much appreciated.

/*jshint multistr:true */

var text = "Yada yada Maarten yada yada yada Maarten";

var myName = "Maarten";

var hits = [];

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


#2

This is my code:

var text = "blob blob blob Ryan blob blob Ryan blob blob blob blob blob blob blob blob Ryan blob";
var myName = "Ryan";
var hits = [];

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

console.log(hits);

I'm guessing that it is crashing because on your second for loop, you put

for(var j = i; j+(myName.length + i); j++)

instead of

for(var j = i; j<(myName.length + i); j++)

Because of putting + instead of <, and you made the j go constantly upwards until the requirement is done, the j keeps on adding with the myName.length and i, crashing the browser.

Hope it helps!


#3

Thank you very much, this really helps me out in understanding Javascript!