I cannot find the mistake in the following code
function doArray(n){ // test with n > 1000
var str, i, j,arr=[], cnt=0, q=0, re;
str = ["one two three","two three four","three four five", "four five one", "five one two"];
for(i=0; i<n; i++){
j = Math.round(Math.random()*(str.length-1));
arr.push(str[j]);
}
re =/two/g;
for(i=0; i<n; i++){
if(re.test(arr[i])){
cnt++;
};
if(arr[i].indexOf("two") >-1 ){
q++;
}
}
alert("found with .test:"+cnt+", and width indexOf:"+q) ;
}
It is synthesized to find the problem, but it persist.
If you test it by your own, you will see that results are different for 'cnt' and 'q'