简体   繁体   English

在JavaScript中不正确地填充和未定义2D数组

[英]2D array improperly filled and undefined in JavaScript

I am writing a function with two sub-functions: one to print a 2D array from two values in the form and the other to print out the 2D array. 我正在编写一个具有两个子函数的函数:一个用于从表单中的两个值打印二维数组,另一个用于打印出二维数组。 At this point, I have been trying to use variables with integer values to fill in the 2D array, but they are not being filled properly. 此时,我一直在尝试使用带有整数值的变量来填充2D数组,但它们没有被正确填充。 The min variable seems to have the value of 0 within the function instead of what is being passed. min变量似乎在函数中的值为0而不是传递的值。

Anyways after I create the 2D array, I try to test print the 2D array 'twoDarray', but it comes up as undefined? 在我创建2D数组之后,我尝试测试打印2D数组'twoDarray',但是它是未定义的?

        function isValid(frm) {
            var xValue = document.getElementById("X").value;
            var yValue = document.getElementById("Y").value;
            if (xValue <= yValue)
            {
                /* Draw out the multiplication table. */
                function gen2Darray(min, max)
                {
                    var lengthOf2Darray = (max - min);              
                    var twoDarray = new Array(lengthOf2Darray);
                    for (var i = 0; i < lengthOf2Darray; i++)
                    {
                        twoDarray[i] = new Array(lengthOf2Darray);
                        for (var j = 0; j < lengthOf2Darray; j++)
                        {
                            twoDarray[i][j] = ((min + i) * (min + j)); // only takes i
                        }
                    }
                }   
                gen2Darray(xValue, yValue); 

                function print_2d_string_array (array)
                {
                    // print twoDarray, but twoDarray undefined here                    
                }

I have created a jsfiddle account, but it does not work the same way on the site: http://jsfiddle.net/imparante/5yTEv/ . 我创建了一个jsfiddle帐户,但它在网站上的工作方式不同: http//jsfiddle.net/imparante/5yTEv/

Fix: 固定:

function isValid(frm) {
    var xValue = document.getElementById("X").value;
    var yValue = document.getElementById("Y").value;
    var lengthOf2Darray = (yValue - xValue);
    var twoDarray = new Array(lengthOf2Darray);

    if (xValue <= yValue) {
        /* Draw out the multiplication table. */
        function gen2Darray(min, max) {
            //                      var lengthOf2Darray = (max - min);              
            //                      var twoDarray = new Array(lengthOf2Darray);
            for (var i = 0; i < lengthOf2Darray; i++) {
                twoDarray[i] = new Array(lengthOf2Darray);
                for (var j = 0; j < lengthOf2Darray; j++) {
                    twoDarray[i][j] = ((min + i) * (min + j));
                    $("#table").append(twoDarray[i][j] + " ");
                }
            }
            return twoDarray;
        }
        gen2Darray(xValue, yValue);

        function print_2d_string_array(array){
            // print twoDarray
        }

You're getting undefined on twoDarray because the array is declared and created inside gen2Darray() but you are trying to access it from print_2d_string_array() . 你在twoDarray上得到了未定义,因为数组是在gen2Darray()声明和创建的,但是你试图从print_2d_string_array()访问它。

If you move your var twoDarray = new Array(); 如果移动你的var twoDarray = new Array(); up to you xValue and yValue declarations that should solve the undefined error. xValueyValue声明来解决未定义的错误。

Bin example Bin的例子

function isValid(frm) {
    var xValue = document.getElementById("X").value;
    var yValue = document.getElementById("Y").value;
    var lengthOf2Darray;
    var twoDarray = [];

    if (xValue <= yValue){
        /* Draw out the multiplication table. */
        gen2Darray(xValue, yValue);
        print_2d_string_array(twoDarray);
    }

    function gen2Darray(min, max){
        lengthOf2Darray = (max - min);              
        twoDarray = [lengthOf2Darray];
        for (var i = 0; i < lengthOf2Darray; i++){
            twoDarray[i] = new Array(lengthOf2Darray);
            for (var j = 0; j < lengthOf2Darray; j++){
                twoDarray[i][j] = ((min + i) * (min + j)); // only takes i
            }
        }
    }

    function print_2d_string_array(array){
        console.log(array);
    }
}

The declarating of the functions were a bit wrong, also the function gen2Darray was not actually returning anything, so the variable twoDarray would only be available in the scope of the function. 声明函数有点错误,函数gen2Darray实际上并没有返回任何内容,因此变量twoDarray只能在函数范围内使用。

Please see fixed code here: http://jsfiddle.net/CC5vP/ 请在此处查看固定代码: http//jsfiddle.net/CC5vP/

I moved the two functions out of the main function declaration and then added a return in gen2Darray(min, max) 我将两个函数移出main函数声明,然后在gen2Darray(min, max)添加了一个返回gen2Darray(min, max)

It appears to be working, you will want to modify print_2d_string_array(a) to display the data instead of logging it to the console. 它似乎正在工作,您将需要修改print_2d_string_array(a)以显示数据,而不是将其记录到控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM