5. Your Second "For"Loops - I don't don't know where I'm going wrong :/


#1

/*jshint multistr:true */
var text = "My name is Mark and I hate JavaScript";
var myName = "Mark";
var hits = [];
    for (var i = 0; i < text.length; i++); 
    if (text[i] === "Mark"); {
        for (var j = i; j < i + myName.length; j++); {
            hits.push(text[j]);
            console.log(hits);
        }
    }

#2

Problem is:

for (var i = 0; i < text.length; i++); // here
    if (text[i] === "Mark"); {
        for (var j = i; j < i + myName.length; j++); { // and here
            hits.push(text[j]);
            console.log(hits);
        }
    }

Review your for loop:

for (statement 1; statement 2; statement 3) {
    code block to be executed
}

Also you don't need console.log(hits); inside the for loop, because it prints every iteration. Replace it outside the for loop.


#3

Do what @denys.matsevych told

also here

if (text[i] === "Mark"); { // remove ;

; after if condition is a bad idea

and text[i] holds only single character so it'll never be true


#4

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