1

Im trying to code a random houses generator in JS for a game of thrones game, the code looks like that at the moment:

//Houses Array:
var HousesArray = [
 'Stark',
'Lanister',
'Greyjoy',
'Barathion',
'Arryn',
'Tyrell',
'Martell',
'Targaryen'
];

//Houses Variables:
var house1 = {}
var house2 = {}
var house3 = {}

//Random House Generator
var RandomIndex = Math.floor(Math.random()*HousesArray.length)
var RandomElement = HousesArray[RandomIndex]

//3 Players Generator:
 house1 = RandomElement
   console.log(house1)

   HousesArray.splice(house1,1)
   RandomElement = HousesArray[RandomIndex]
   house2 = RandomElement
   console.log(house2)
   
   HousesArray.splice(house2,1)
   RandomElement = HousesArray[RandomIndex]
   house3 = RandomElement
   console.log(house3)

What I tryed to do here is to give the house1 variable a random house name from one of the HousesArray Eelements, echo that to the console log, then remove the selected element from HousesArray using the splice commend (to ensure that the next selection will not be the same one). after all that - generate new house name from the elements left in the HousesArray and repeat. But for some reasos, Every time house1 = "Targaryen" (or house2) the next 2/1 resolts automaticly return "undifined" (I beleive its becuse the "Targaryen" element is the last in the list)

How can i fix that?

  • 1
    The `.splice()` function expects the first two arguments to be **numbers**. – Pointy Mar 01 '19 at 14:44
  • Also if you don't re-compute `RandomIndex` and `RandomElement` they will not change by themselves. – Pointy Mar 01 '19 at 14:45
  • So much easier to randomly shuffle the array to start and than just pop them off the array. https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array – epascarello Mar 01 '19 at 14:57

3 Answers3

0

Doesn't appear you're using the splice method correctly here, it takes the first 2 arguments as numbers, and the third argument would be an element you want to replace it with if you'd like. Instead I would use a filter here:

let newHouseArray = HousesArray.filter(house => house !== house1)
Dmitriy
  • 1,211
  • 1
  • 11
  • 28
0

Your code is not working because RandomIndex is always the same, if it's 7, it will return undefined because splice modifies the original array by removing the found House,

change RandomIndex to be a function to call everytime so it generates a new index using the new length of the array:

var RandomIndex = () => Math.floor(Math.random() * HousesArray.length)

//Houses Array:
var HousesArray = [
  'Stark',
  'Lanister',
  'Greyjoy',
  'Barathion',
  'Arryn',
  'Tyrell',
  'Martell',
  'Targaryen'
];

//Houses Variables:
var house1 = {}
var house2 = {}
var house3 = {}

//Random House Generator
var RandomIndex = () => Math.floor(Math.random() * HousesArray.length)
var RandomElement = HousesArray[RandomIndex()]

//3 Players Generator:
house1 = RandomElement
console.log(house1)

HousesArray.splice(HousesArray.indexOf(house1), 1)
RandomElement = HousesArray[RandomIndex()]
house2 = RandomElement
console.log(house2)

HousesArray.splice(HousesArray.indexOf(house2), 1)
RandomElement = HousesArray[RandomIndex()]
house3 = RandomElement
console.log(house3)
Taki
  • 17,320
  • 4
  • 26
  • 47
0

This kept me stumped for quite a while, I would do something like this instead:

Now if you console.log(houses) you will get 3 random houses from random positions (as stated in the above comment) allowing players 2 and 3 to get houses earlier in position than players 1 and 2

Hope this helps :) seem's like a fun game:

//Houses Array:
var HousesArray = ['Stark', 'Lanister', 'Greyjoy', 'Barathion', 'Arryn', 'Tyrell', 'Martell', 'Targaryen'];

// stores the houses
var houses = [];

// Generates 3 random numbers and allows players 2 and 3 to get the houses early in position than player 1 and 2
var arr = [];
while (arr.length < 3) {
    var r = Math.floor(Math.random() * 8);
    if(arr.indexOf(r) === -1) arr.push(r);
}

// Pushed the houses to the house array
for (var x = 0; x < arr.length; x++) {
  houses.push(HousesArray[arr[x]])
} 

console.log(houses);
CodeBoyCode
  • 2,227
  • 12
  • 29