Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:10:17+00:00 2026-06-17T13:10:17+00:00

I have a very simple part of my program here that’s having a problem…

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:10:19+00:00Added an answer on June 17, 2026 at 1:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple program that I am trying to improve performance. One
I have coded a very simple Word Count program that reads a file and
I have a very simple script that creates a user: <?php include 'mysqlserver.php'; session_start();
I have a very simple case that I think would benefit from using templates
I have a very simple bit of code that won't work, and have no
I am trying to create a very simple application (part of my learning program),
I have a very simple program in swi-prolog, which in I define all predicates
I have a very simple application that works but when i add roboguice it
I have very simple piece of code. The goal is when i input four-digit
I have very simple OpenGL ES example similar to Hehe's example : http://nehe.gamedev.net/tutorial/ios_lesson_02__first_triangle/50001/ As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.