2

Creating a small application to generate random numbers. See the fiddle here.

Having an issue with the function that generates the numbers. There are 3 variables that control the output. The count of the numbers in a set/sequence, the number of the sets, and a number to limit the generated number, max number.

I created 2 loops to generate the numbers in sets. The inner loop creates a set of numbers (an array), and the outer loop adds that set to setArr, another array.

So I created 2 arrays. The one that will hold the current generated set temp, and the one that will hold the whole sets, setsArr.

Inner loop creates a set, and the outer loop pushes the created set to setsArr. And before executing again I empty the temp array. So this goes on and on.

But this doesn't work. I must be missing something. Any help?

function gen() {
    var cols = document.getElementById("cols").value;
    var sets = document.getElementById("sets").value;
    var max = document.getElementById("max").value;

    var setsArr = [];
    var temp    = [];

    for (var i = 0; i < sets; i++) {
        for (var j = 0; j < cols; j++) {
            var num = Math.floor(Math.random() * max);
            temp.push(num);
        }
        setsArr.push(temp);
        temp.length = 0;
    }

    console.log(setsArr);
}
1
  • What doesn't work about it? At a glance, it looks like temp should be declared in the outer loop, and not outside both loops. Commented Mar 24, 2014 at 20:21

5 Answers 5

2

When you're pushing the temp array to setsArr, it is not copied but referenced. Emptying it will empty the array in setsArr. Also when always pushing the same array, you will get setsArr[0] == setsArr[1]

Instead, create a new array object on every iteration:

var setsArr = [];

for (var i = 0; i < sets; i++) {
    var temp = [];
    for (var j = 0; j < cols; j++) {
        var num = Math.floor(Math.random() * max);
        temp.push(num);
    }
    setsArr.push(temp);
}
Sign up to request clarification or add additional context in comments.

Comments

1

jsFiddle Demo

Setting the length of the array to 0 was affecting every array placed into the parent array. Instead, use a local variable there, and it will create a new instance to use each time. This will properly allow for unique arrays.

var setsArr = [];

for (var i = 0; i < sets; i++) {
    var temp = [];//local array instantiated
    for (var j = 0; j < cols; j++) {
        var num = Math.floor(Math.random() * max);
        temp.push(num);
    }
    setsArr.push(temp);//save unique array
}

Comments

0

The problem is this line:

temp.length = 0;

Setting the length of the array to 0 wipes out its contents. That's why your arrays are empty.

2 Comments

He said he wanted to "empty the temp array". Why shouldn't he?
What he wants is to set temp to be a new empty array (temp = []) not empty out the array he just pushed into setsArr which is what the code is currently doing.
0

DEMO

use local variable and then delete the local variable and do not wait for garbage collector.

for (var i = 0; i < sets; i++) {
    var temp = [];
    for (var j = 0; j < cols; j++) {
        var num = Math.floor(Math.random() * max);
        temp.push(num);
    }
    setsArr.push(temp);
    delete temp;
}

Comments

0
var generate    = document.getElementById("generate");
generate.onclick = gen;

function gen() {
var cols = document.getElementById("cols").value;

var sets = document.getElementById("sets").value;
var max = document.getElementById("max").value;

var setsArr = new Array();

for (var i = 0; i < sets; i++) {
    setsArr[i] = new Array(); 
    for (var j = 0; j < cols; j++) {
        var num = Math.floor(Math.random() * max);
        setsArr[i][j]=num;
    }
}
console.log(setsArr);
}

Now its working fine i updated your fiddle

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.