0

I have some functions which returns an array. These functions will be called by another function. My code doesn't seem to work, any idea why?

function foo1(){
    ..
    ..
    var A[];

    return A;
}

function foo2(){
    ..
    ..
    var B[];

    return B;
}

function catcher(){

    c1=foo1();
    c2=foo2();

    var d=[];
    for(var i = 0; i<a.length; i++){
        d.push(a[i],b[i],c[i]);
    }

}

Could anybody throw some light on how to access one function from another. I suspect it's not storing what I want it too (in the array).

2
  • You should not make syntactial errors in your code which you post. It creates confusion whether you are facing issues due to syntactical error or is there any other reason. Commented Nov 25, 2013 at 11:48
  • See my answer, the answer you accepted does not include a vital part needed in your code. Commented Nov 25, 2013 at 11:59

3 Answers 3

1

var A[] is probably giving you a javascript error. Use var A=[] as you are doing with var d=[]. This obviously applies to var B[] as well.

Also, your loop seems to be referencing variables that don't exist in your example code - a, b and c - is this part of the problem?

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

2 Comments

nope Please ignore all these I haven't made those mistakes actually.. I just gave an example..!!
Out of interest which part of my answer was it that helped you then?
0

I don't think there is any other issue apart from Syntax errors. Check this fiddle on JS Bin Fiddle

function foo1() {
  var a = [1,2,3,4,5];
  return a;
}

function foo2() {
  var b = [6,7,8,9,10];
  return b;
}

function init() {
  var a1 = foo1();
  var b1 = foo2();

  var d = [];
  for (var i=0; i<a1.length; i++) {
    d.push(a1[i], b1[i]);
  }

  console.log(d);
}

init();

Output: [1, 6, 2, 7, 3, 8, 4, 9, 5, 10] // As Expected

Let me know if you need something else.

1 Comment

thanks actually my code worked too. Don't why it didn't worked earlier though.
0

There is a couple of things wrong here (and some things that might go wrong).

First off, you'll need to really declare the arrays, so instead of var A[]; use var A = [];

Second, in your for loop you are checking the a array's length, which would make the code "break" if the other arrays have more (or less) objects inside of them.

I've written an example based on your example code:

function foo1(){
    var A = ['a', 'is', 'here'];
    return A;
}

function foo2(){
    var B = ['b', 'is', 'here'];
    return B;
}

function foo3(){
    var C = ['c', 'is', 'here'];
    return C;
}

function catcher(){
    var a = foo1();
    var b = foo2();
    var c = foo3();

    var ABC = [];

    //Run for each array, so that it docent break:

    for (var i = 0; i < a.length; i++) {
        ABC.push(a[i]);
    }

    for (var i = 0; i < b.length; i++) {
        ABC.push(b[i]);
    }

    for (var i = 0; i < c.length; i++) {
        ABC.push(c[i]);
    }

    //Print out the result (to verify that it worked):

    for(var i = 0; i<ABC.length; i++){
        console.log(ABC[i]);
    }
}

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.