开发者

array passed as parameter issue

I've got 2dim array set as global variable populated with numbers on row - 0 and strings on row-1. But when I passed it as a parameter to a function most of its values modifies into undefined but in one strangely value is kept!?!

    function formElements(howMany){ 
        e开发者_运维技巧lArr = [];
        var w; var surface;
        for(var j=0; j<howMany; j++){
           w = randomNumber(1,200);
           surface = w*randomNumber(1,100);
           elArr[0] =[j];
           elArr[1] =[j];
           elArr[0][j]=surface;   
           elArr[1][j]='_'+j;
        }
        aFunction(elArr,...other parameters....); //in this function I receive array with these undefined values I mentioned above!!!
    }
    function randomNumber(x,y) {
        return Math.floor((Math.abs(y - x) + 1) * Math.random()) + Math.min(x, y);  
    }

Could somebody tell me what's wrong? 10x and BR


The references to j make it look like this is inside a loop. In which case, I don't think this is doing what you want:

elArry[0] = [j];

This sets the value of elArray[0] to an array with a single element, j. If you're doing that inside your loop then you're overwriting the arrays every time with a new one with a single element.

EDIT:

And now that you've posted the full loop that's verified. You probably want something like:

function formElements(howMany){ 
    elArr = [[],[]];
    for(var j=0; j<howMany; j++){
       elArr[0][j]=surface;   
       elArr[1][j]='_'+j;
    }
    aFunction(elArr,...other parameters....);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜