Is there a bug on the compiler?


#1

Please see my code below and my comment in the code. I also put some console.log as debugger, to see where and how actually my code run. Thank you for spending time checking my code.

/*jshint multistr:true */
var text = "Johan Johan Johan";
var myName = "Johan" ;
var hits = [];

for (var i = 0; i < text.length; i++) {
if (text[i] === "J") {
// BUG: it skips the second for-loop, whenever we pushed the first "Johan" in hits Array. As we can see, the text is "Johan Johan Johan", and the hits Array supposed to had [J,o,h,a,n,J,o,h,a,n,J,o,h,a,n]. But hits Array did not.
for (var j = i; j < (myName.length + 1); j++) {
console.log("debug: push: " + text[j]);
hits.push(text[j]);
}
console.log("debug: on " + i + ": " + text[i]);
console.log("debug: i :" + i);
}
}
console.log(hits);


#2

@johandejesus,

Have a close look at the condition in your 2nd FOR-loop

j < (myName.length + 1)


#3

I see it now.
After the first "Johan" pushed into hits Array, the variable j, that is equal to variable i, is incrementing, while my condition in second-loop is stucked at 6 (that is myName.length +1); therefore, the condition in second-loop is never fulfilled and is never gets executed. i.e. 7 < 6 (false condition)
Thank you @leonhard.wettengmx.n