saratogacoach
saratogacoach

Reputation: 160

Merge and alternate 2 arrays into single array

I have beginner scripting skills and am using a form of JavaScript, ECMA-262 that is found in a program called Opus Pro (Digital Workshop, UK).

I've been searching online, without success, to find a way to merge and alternate number elements from 2 arrays into a third merged array. I've found a few scripts on this site, but they use functions like "push" which are not found in this script language, so can't be used.

My current script which creates one array for a set of randomly chosen numbers, (math.random),1-6, needs to be modified to create the 2 arrays (1-3 and 4-6), then alternately merge a value from the first array with a value from the second array, until the new array has 6 values taken from these (sub-) arrays:

current, unmodified script:

function separate()
{
  for(i=1;i<=listamount;i++)
     {
     //create 6 random numbers from 1-6
     temp = Math.round(Math.random()*(6))
     if (temp == 0){temp = 1}
     if (temp == 7){temp = 6}

        randomset[i] = temp
     }         
}

This needs to be revised so that the final randomlist array (call it newrandomlist[i]) would be an alternating mix like 2,6,1,4,3,5, but merged from 2 randomly ordered/created sub-arrays (1-3 and 4-6). Alternating values, from each randomly ordered/constructed sub-array, are critical to the functioning of the rest of the scripts in the project. I've tried any number of ideas, all unsuccessfully.

Any help appreciated.

Upvotes: 3

Views: 3140

Answers (1)

jfriend00
jfriend00

Reputation: 707436

ECMA-262 has the Array object which has the .push() method. You can see it yourself in the ECMA spec in section 15.4.4.7.

To merge two arrays by alternating between random elements from each array, you can do this:

function mergeTwoRandom(arr1, arr2) {

    function extractRandom(arr) {
        var index = Math.floor(Math.random() * arr.length);
        var result = arr[index];
        // remove item from the array
        arr.splice(index, 1);
        return(result);
    }

    var result = [];
    while (arr1.length || arr2.length) {
        if (arr1.length) {
            result.push(extractRandom(arr1));
        }
        if (arr2.length){
            result.push(extractRandom(arr2));
        }
    }
    return(result);
}

If you want to do it without .push(), you can do it like this:

function mergeTwoRandom(arr1, arr2) {

    function extractRandom(arr) {
        var index = Math.floor(Math.random() * arr.length);
        var result = arr[index];
        // remove item from the array
        arr.splice(index, 1);
        return(result);
    }

    var result = [];
    while (arr1.length || arr2.length) {
        if (arr1.length) {
            result[result.length] = extractRandom(arr1);
        }
        if (arr2.length){
            result[result.length] = extractRandom(arr2);
        }
    }
    return(result);
}

If you also don't have .splice(), you can do it like this:

function mergeTwoRandom(arr1, arr2) {

    function removeItem(arr, index) {
        for (var i = index; i < arr.length - 1; i++) {
            arr[i] = arr[i + 1];
        }
        arr.length = arr.length - 1;
    }

    function extractRandom(arr) {
        var index = Math.floor(Math.random() * arr.length);
        var result = arr[index];
        // remove item from the array
        removeItem(arr, index);
        return(result);
    }

    var result = [];
    while (arr1.length || arr2.length) {
        if (arr1.length) {
            result[result.length] = extractRandom(arr1);
        }
        if (arr2.length){
            result[result.length] = extractRandom(arr2);
        }
    }
    return(result);
}

Upvotes: 3

Related Questions