-1

I have 2 arrays with 4 x-coordinates in each. In total I want to select 3 random values from both arrays. So array1+array2 =3 elements. In the end i just done it a bad way to make it do what i wanted. Just wondering if anyone can see a more efficient way to produce nicer code. This is how i ended up getting it to work. I cant show what i have tried as it has been so modified i ended up just deleting it and doing this.

enemyCars = [100, 200, 300, 400];    //not actual values

newCarArray = [];    //global
newWallArray = [];    //global

function randomNumber(a, b){
    return Math.round(random(a,b));
}

function chooseCars(){
    let carAmount = randomNumber(0,3);
    let wallAmount = 3 - carAmount;
    print('carAmount ' + carAmount);

    if(carAmount == 0){
        newCarArray = [];
    }
    if(carAmount == 1){
        let a = randomNumber(0,3);
        newCarArray.push(enemyCars[a]);
    }
    if(carAmount == 2){
        let a = randomNumber(0,3);
        newCarArray.push(enemyCars[a]);
        let b = randomNumber(0,3);
        newCarArray.push(enemyCars[b]);
    }
    if(carAmount == 3){
        let a = randomNumber(0,3);
        newCarArray.push(enemyCars[a]);
        let b = randomNumber(0,3);
        newCarArray.push(enemyCars[b]);
        let c = randomNumber(0,3);
        newCarArray.push(enemyCars[c]);
    }

    if(wallAmount == 0){
        newWallArray = [];
    }
    if(wallAmount == 1){
        let a = randomNumber(0,3);
        newWallArray.push(enemyWalls[a]);
    }
    if(wallAmount == 2){
        let a = randomNumber(0,3);
        newWallArray.push(enemyWalls[a]);
        let b = randomNumber(0,3);
        newWallArray.push(enemyWalls[b]);
    }
    if(wallAmount == 3){
        let a = randomNumber(0,3);
        newWallArray.push(enemyWalls[a]);
        let b = randomNumber(0,3);
        newWallArray.push(enemyWalls[b]);
        let c = randomNumber(0,3);
        newWallArray.push(enemyWalls[c]);
    }
}

thanks for the help

Liam
  • 27,717
  • 28
  • 128
  • 190

3 Answers3

1

Thanks for the help I was finally able to create some nicer code. always seem to overcomplicate things and can't see the simple solution. Thanks @JonasWilms

function abc(){
    let carAmount = randomNumber(0,3);
    let wallAmount = 3 - carAmount;
    print('carAmount ' + carAmount);
    print('wallAmount ' + wallAmount);
    let enemyCoord = [...enemyCars];
    //print(enemyCoord);
    newCarArray = [];
    newWallArray = [];

    for(let i = 0; i < carAmount; i++) {
        let a = randomNumber(0,enemyCoord.length-1)
        newCarArray.push(enemyCars[a]);
        delete enemyCoord[a];
        filterArray(enemyCoord);
    }
    for(let i = 0; i <wallAmount; i++){
        let a = randomNumber(0,enemyCoord.length-1)
        newWallArray.push(enemyWalls[a]);
        delete enemyCoord[a];
        filterArray(enemyCoord);
    }
    print(newCarArray);
    print(newWallArray);
}
1

Here's a short code that merges two arrays into a single array, shuffles it randomly, and then give you the first three values from the array:

var randoms = randoSequence([1, 2, 3, 4].concat([5, 6, 7, 8])).splice(0, 3)
randoms.forEach(function(p, i, a){a[i]=a[i]["value"]});

console.log(randoms);
<script src="https://randojs.com/1.0.0.js"></script>

Notice the second line. https://randojs.com gives you an array of objects with BOTH indices and values, but we're only interested in the values.

Aaron Plocharczyk
  • 2,776
  • 2
  • 7
  • 15
  • Am I correct to assume I can replace the [1,2,3,4] & [5,6,7,8] with the array variable name of each array and then the splice starts at index 0 and removes the first 3 from the array and stores them in randoms variable? – Matthew Reid Dec 17 '19 at 13:59
0

All you need is a loop:

 for(let count = 0; count < carAmount; count++) {
    newCarArray.push(enemyCars[ randomNumber(0, 3) ]);
 }
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151