1

All the relevant code is provided here http://jsfiddle.net/DuWGj/ , as well as print(appendTo) statements.

To keep it short, I'm making 4 arrays. Each array has 4 numbers. Then I create a new array that has all those 4 arrays numbers inside of it, so it's one array.

For example the end result is

four.myArraysCombined = [5,3,20,12,3,4,18,11,12,5,8,2,1,9,10,6];

However, when I try

four.myArraysCombined[3] , it says it's undefined.

so obviously when I do the following it doesn't work

var total = 0;
for (var x = 0; x < 16; x++) {
    total += four.myArraysCombined[x]);
}

I'm looking to be able to add all those numbers together with a for loop. I've tried several things but it keeps giving me undefined or NaN.

3
  • 2
    have you done a console.log(four.myArraysCombined) to see if you really are generating the array you think you have? Commented Jan 14, 2014 at 16:35
  • You can generate your combined array as simple as: myArrays.join().split(",").map(function(entry) { return Number(entry) }) Commented Jan 14, 2014 at 16:37
  • uhm... i dont' know if is the error... but you push your temp array inside other empty array... try to use = instead push() i edit your fiddle jsfiddle.net/DuWGj/1 - follow the comment in JS Commented Jan 14, 2014 at 16:40

3 Answers 3

3

What is happening

After running:

prePickNumbers(four, 4, 40, 20, 1);

...the value of four.myArraysCombined is:

[[[2, 17, 20, 1], [7, 2, 20, 11], [7, 14, 3, 16], [12, 17, 3, 8]]]

In other words, it is not the result that you claim it is. You should verify that you have the result that you think you do at each step of the process, before moving on. As it stands, you do not have a flattened array. You need to fix that first and then move on to iterating and summing.

Why this is happening

The reason for the final structure starts at the following line in prePickNumbers:

tempMyArraysCombined.push(objName.myArray[x]);

You're pushing an array into another array each time, so the result after the loop is an array of arrays. But, then, you push that result into another array:

objName.myArraysCombined.push(tempMyArraysCombined);

So the final result is actually an array containing an array of arrays (notice the extra set of brackets in the output above). The problem is that you're pushing an entire array into your output at each step in the process, which is creating a nested mess. You should be pushing elements of each array, not the arrays themselves.

How to fix it

Here is one possible solution. Replace prePickNumbers with the following function:

function prePickNumbers(objName, theNum, theSumNum, theMaxNum, theMinNum) {
    var tempMyArraysCombined = [];
    for (var x = 0; x < theNum; x += 1) {
        pickNumbers(objName.myArray[x], theNum, theSumNum, theMaxNum, theMinNum);
        for (var j = 0; j < objName.myArray[x].length; j++) {
            objName.myArraysCombined.push(objName.myArray[x][j]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

the value shown here is an array with one item, that one item is an array with 4 items. notice the three opening square brackets. [[[
1

You may want to try

 total += four.myArraysCombined[0][x]

Comments

1

I extract this from you fiddle:

function prePickNumbers(objName, theNum, theSumNum, theMaxNum, theMinNum) {
    var tempMyArraysCombined = [];
    for (var x = 0; x < theNum; x += 1) {
    pickNumbers(objName.myArray[x], theNum, theSumNum, theMaxNum, theMinNum);
    tempMyArraysCombined.push(objName.myArray[x]);
    }
    objName.myArraysCombined.push(tempMyArraysCombined); 
}

edit the last line to:

function prePickNumbers(objName, theNum, theSumNum, theMaxNum, theMinNum) {
    /* your code */
    objName.myArraysCombined=tempMyArraysCombined; //edit this line not push() but =
}

now there are no "UNDEFINED" in output html. :)

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.