1

I want to push arrays containing random numbers (0 to 10) into a bigger array once the total of its contents is about to exceed 30. But the output is messed up.

var bigarray = new Array();
var smallarray = new Array();

var randNum = 0;
var total = 0;

for (var i = 0; i<10; i++){

    randNum = (10*Math.random()).toFixed(0);
    total = total + randNum;


    if(total>30) {

        bigarray.push(smallarray)

        smallarray.length=0;
        smallarray.push(randNum);
        total = randNum;

    } else {

        smallarray.push(randNum);

    }

}

alert(" BIG ARRAY IS "+bigarray);
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80

4 Answers4

0

I made changes to you code and came up with this.

var bigarray = [];
var smallarray = [];

var randNum = 0;
var total = 0;

for (var i = 0; i < 10; i += 1) {
    randNum = Math.floor(10 * Math.random()); // you will never have a value of 10?
    total = total + randNum;

    if (total > 30) {
        bigarray.push(smallarray.slice())
        smallarray.length = 0;
        smallarray.push(randNum);
        total = randNum;
    } else {
        smallarray.push(randNum);
    }
}

alert(" BIG ARRAY IS " + bigarray);

On jsfiddle

Things I changed were:

Ran the code through a beautifier

Changed your use of new Array to []

{} and []

Use {} instead of new Object(). Use [] instead of new Array().

Because Object and Array can be overwritten by the user

Changed ++ to += 1

This pattern can be confusing.

Check out Code Conventions for the JavaScript Programming Language and jslint

Added array.slice when you push smallarray to bigarray, this makes a copy in this case. It is important to understand how javascript works, read Is JavaScript a pass-by-reference or pass-by-value language? Without using slice, which makes a copy as the array only contains primitives, when you set the length of the array to 0, then the data was lost.

Changed your use of number.toFixed to Math.floor so that randNum remains a number

Note: Math.random returns a floating-point, pseudo-random number in the range [0, 1] that is, from 0 (inclusive) up to but not including 1 (exclusive)

Whether your code now produces your expected out, I can not be sure from your description but this should be a good starting point.

Community
  • 1
  • 1
Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • it was awesome! thanks a ton. so, what happens when you use push instead of slice with arrays ? does 'push' act differently when it comes to arrays ? – Cibi Kulandaisamy Apr 29 '13 at 02:23
  • I updated the answer with link and some further explanation to try an clarify the situation for you. – Xotic750 Apr 29 '13 at 06:16
0

two wrong things are visible on the first sight in the code

(1) instead of

randNum = (10*Math.random()).toFixed(0);

you probably want

randNum = Math.floor(11*Math.random());
  • Math.floor instead of toFixed() - see @kennebec comment
  • 11 instead of 10 to return numbers 0 to 10, as 0 <= Math.random() < 1

(2) the following line pushes (many times) the reference to the same smallarray object.

bigarray.push(smallarray);

In the next step you clear the array with smallarray.length = 0. Because the array is not copied to the bigarray, but only referenced, the generated items are lost.

EDIT: I read your question wrong - the rest of the answer is fixed

You probably want to push the duplicate of the smallarray into bigarray, so replace the line above with the following:

bigarray.push(smallarray.slice(0));
petrch
  • 532
  • 3
  • 6
0
    var bigarray = new Array();
    var smallarray = new Array();
    var randNum = 0;
    var total = 0;
    for (var i = 0; i < 10; i++) {
        for (var j = 0; j < smallarray.length; j++) {
            total = total + smallarray[j];
        }
        if (total <= 30)
        { 
            randNum = Math.floor((Math.random() * 10) + 1);
            smallarray.push(randNum);
        }
        else {
            bigarray.push(smallarray.slice(0));                
            smallarray.length = 0;
        }
        total = 0;    

    }
    alert(" BIG ARRAY IS " + bigarray);
Jagan KS
  • 1
  • 1
0

You need another loop inside the main one to populate the smallarray, something like:

var bigarray = new Array();

for (var i = 0; i<10; i++){

    // moving the variable declarations inside this loop means they are re-set for each small array
    var smallarray = new Array();
    // create the first entry for the small array
    var randNum = Math.floor(11*Math.random());
    var total = randNum;

    // loop to populate the small array
    while(total <= 30){
        smallarray.push(randNum);
        randNum = Math.floor(11*Math.random());
        total += randNum;
    }
    bigarray.push(smallarray)
}
kevmc
  • 1,284
  • 7
  • 8