Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a very simple part of my program here that's having a problem... The elements in the array 'population' are also an array.. having 28 random numbers each 'temp' array that is being loaded to the array population. My problem is that the array 'population''s somehow have all the same arrays saved in it, it's like its being override every loop. Ive spent so much time in this very simple problem, is this some kind of bug?? The commented 'alert' is used to check the element 0 and 1 of the population. and somehow it's really being overriden every loop so every temp elements in the population array is all the same. Please help me..

var population[];
function init_population(){   
    temp = [];
    //Math.floor(Math.random()*8);
    for(i=0;i<10;i++){
        for(j=0;j<28;j++)
        temp[j] = Math.floor(Math.random()*8);
        population[i]= temp;
    //alert("population[0] = " +population[0] +" and population[1] = " +population[1]);
    }
}

init_population();
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

You need to create a new temp array in the inner loop so you're not reusing the same array over and over:

var population = [];

function init_population() {   
    var temp, i, j;
    for(i=0; i<10; i++) {
        temp = [];
        for(j=0; j<28; j++) {
            temp[j] = Math.floor(Math.random()*8);
        }
        population[i] = temp;
    }
}

init_population();

Since assigning the temp array into the population array only puts a reference to the temp array in, when you keep using the same temp array over and over again, you end up with references to the same temp array at each index of the population array. If, instead, you create a new temp array in the inner loop, then each array in the population array will be different.


FYI, I also made some other corrections to your code to properly declare the variables temp, i and j as local variables so they aren't implicit global variables.

share|improve this answer
    
Thank you so much for the answer, it really helped, but just to make me understand, i thought the temp array is just gonna be overridden since the j counter of the inner loop again is back to 0 once the outer loop iterates? –  Wilmor Herald Jan 21 '13 at 7:28
    
@WilmorHerald - you overwrite the values of the temp array, but you put a pointer to the same temp array in every element of the population array so every element in the population array is the same. Instead, you have to create a new temp array with temp = []; inside the first loop so that each element of population contains a different array. If you still don't understand, then you need to learn about assignment "by reference" and "by value" in javascript. Google will be your friend. –  jfriend00 Jan 21 '13 at 7:59
    
Oh, now i understand clearly. Thank you so much, appreciate it. After i've understand it, i realized in a snap that my code was really idiotic. haha.. Thanks to you. –  Wilmor Herald Jan 21 '13 at 9:03
add comment

This is because it's the same array. You create it with temp = [] and then assign it to each of the population[i] for i=0..9

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.