Highlighted

store dynamic array values as an array and pass it to another array

Explorer ,
Jun 25, 2020

Copy link to clipboard

Copied

Hi,

I have this code:

for (var i = 0; i < 5; i++) {
    var count = 0;
    var loopEnd = Math.floor(Math.random() * 5 + 1);
    for (var j = 0; j < loopEnd; j++) {
        count++;
    }
    //alert(count);
}

var arr = Array();
function arrAdd(count){
    arr.push(count);
}
alert (arr.length);

that gives me only one element. I'd need to be able to get all the 5 numbers randomized in the new array. Is there a way to do that?

Cheers

Topics

How to, Scripting

Views

46

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

store dynamic array values as an array and pass it to another array

Explorer ,
Jun 25, 2020

Copy link to clipboard

Copied

Hi,

I have this code:

for (var i = 0; i < 5; i++) {
    var count = 0;
    var loopEnd = Math.floor(Math.random() * 5 + 1);
    for (var j = 0; j < loopEnd; j++) {
        count++;
    }
    //alert(count);
}

var arr = Array();
function arrAdd(count){
    arr.push(count);
}
alert (arr.length);

that gives me only one element. I'd need to be able to get all the 5 numbers randomized in the new array. Is there a way to do that?

Cheers

Topics

How to, Scripting

Views

47

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mylenium LATEST
Most Valuable Participant ,
Jun 26, 2020

Copy link to clipboard

Copied

You have to build the array inside the main loop. Of course the loop itself will only return the last valid value.

 

Mylenium

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...