0

Meet with a really weird javascript problem. See my codes below:

function initBadScripts(controlArray) {
    var scriptsLine = prompt("Please enter the bad scripts", "debug");

    if (scriptsLine != null) {
        var pattern = /;/;
        var nameList = scriptsLine.split(pattern);
        alert(nameList+" "+nameList.length);
        for(var counter = 0; counter < nameList.length; counter++){
           controlArray[counter][0]=true;
           controlArray[counter][1]= new RegExp(nameList[counter],"g");
           alert(controlArray[counter][0]);
        }
    }
    alert("wtf!");
}

var controlArray = [[]];
initBadScripts(controlArray);

I defined a function, and call that function. A 2-dimensional array called 'controlArray' is defined with no value. Basically, the function check the user's input and use regular expression to make a 'namelist'. For example, if the user type in

ExampleOne;ExampleTwo

The function will create an array called 'nameList'

nameList=[ExampleOne,ExampleTwo];

Then I want to make a dynamical initialization of the 2-dimensional array called 'controlArray', according to the length of nameList. However this only works fine the nameList'length is 1. If it exceeds one (the user type in 'ExampleOne;ExampleTwo'), the ExampleTwo does not go into the array, and the

alert("wtf");

doesn't run at all. This seems that there is already an error before it. Any comments?

4
  • 1
    Use your dev tools & debugger to find the error. Commented Sep 17, 2014 at 18:53
  • Sorry I am writing an grease monkey scripts, which can not be usually debugged. Commented Sep 17, 2014 at 18:56
  • var pattern = /;/; should probably be var pattern = /;/g; or else it will only pick up first ; in input resulting in an array of max size 2 Commented Sep 17, 2014 at 18:56
  • That's not the problem. Commented Sep 17, 2014 at 19:05

2 Answers 2

2

JavaScript doesn't have a true 2-dimensional array. Rather, you're putting a second array inside the first array. Change it to this:

...
for(var counter = 0; counter < nameList.length; counter++){
       controlArray[counter] = [true, new RegExp(nameList[counter],"g")];
...
Sign up to request clarification or add additional context in comments.

2 Comments

This works for me. Thanks a lot. By the way, what do you mean that I am putting a second array inside the first array?
If we coded it differently, you could fill it like this: var child1 = [true,/abc/g]; var child2 = [true,/def/g]; var controlArray = [child1, child2]; You have nested the child arrays into the parent array. This controlArray and the controlArray above would be identical.
0

Yes or you declare your variable like that:

var controlArray = [[],[]];

or

var controlArray = new Array(2);
for (var i = 0; i < 2; i++) {
  controlArray[i] = new Array(2);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.