0

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. The min variable seems to have the value of 0 within the function instead of what is being passed.

Anyways after I create the 2D array, I try to test print the 2D array 'twoDarray', but it comes up as undefined?

        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/.

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
        }
2
  • Can you create a fiddle of the issue you are having, the code above is incomplete, we need the HTML, and the entire javascript function. Commented Apr 21, 2014 at 21:28
  • Function declarations inside a conditional are not allowed in strict mode, and in non-strict mode can produce different results in different browsers. Commented Apr 21, 2014 at 21:41

2 Answers 2

2

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().

If you move your var twoDarray = new Array(); up to you xValue and yValue declarations that should solve the undefined error.

Bin example

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Please see fixed code here: http://jsfiddle.net/CC5vP/

I moved the two functions out of the main function declaration and then added a return in 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.

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.