2

I try to read an array using a function and use it in another function, however the second function does not seem to read it. What I am doing wrong?

  function calcDet () {   
            var A = [];     //generates the array
                for (var i = 0; i < k; i++) {
                    A[i] = [];
                    for (var j = 0; j < k; j++) {   
                        var id = "A" + (i + 1) + (j + 1);                   
                        A[i][j] =  parseFloat(document.getElementById(id).value);               
                    }
                }   
                alert (A);
            return (A); 
        }
        function calcRec() {            
                var s;
                var det;
                alert (A)
        }
1
  • thx for all the answers. I can't declare the variable globaly as it's length and values depend on user input. So I have first to read k and then read the array values. I will just use the second approach suggested (A = calcDet()). It works now so thanks a bunch Commented May 18, 2012 at 7:27

5 Answers 5

2

firstly your array is not declared outside the function. its scope is limited to function body. but as your function returns the array back then try using it for initializing your local variable in other array. also this will work. or try declaring it outside the functions

Sign up to request clarification or add additional context in comments.

3 Comments

I expect better than that from a Byakugan.
nw does it complete u r expectations frm a byakugan:)
Now it does. An example would be great
1

Frist way is

function function1()
{
  var valriable1=12;
  function2(val);
}

function function2(val)
{
  var variableoffunction1 = val;
}

Second way is

var globalvarialbe;
function function1()
{
  globalvarialbe=12;
  function2();
}

function function2()
{
  var local = globalvarialbe;
}

Comments

1

It's because JavaScript has a "functional scope". It means that whatever is declared inside the function cannot be seen by the outside, but what's outside can be seen from the inside.

In your case, you must declare the array outside the function.

var A = []

function calcDet(){...}

function calcRec(){
    alert(A);
}

or call the function and return A

function calcDet(){
    var A = [];
    ...
    return A;
}

function calcRec(){
    var A = calcDet(); //get A from calcDet
    alert(A);
}

1 Comment

@Truth updated it. and the OP didn't mention about the code living in the global scope. It could be living in another function or in a closure.
1

You can't because you are declaring A locally in calcDet

One way is to declare A outside the function, or simply feed it into calcRec:

var A
function calcDet() {
   // code
}

function calcRec() {
   // code
}

or (better)

function calcRec() {            
   var s;
   var det;
   alert (calcDet())
}

Comments

1

See this link about scope.

The A variable, being declared inside of the function, is only scoped to that function. It will die after the function dies. You need to pass it as a parameter:

function calcRec(data) {            
        var s;
        var det;
        alert (data)
}
A = calcDet();
calcRec(A);

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.