samedi 18 mai 2019

Picking strings from multiple arrays like a random 'route'

I'm trying to make a 'randomizer' web tool that generates a random result based on various arrays of information by taking a 'route' through a possibilities tree when a button is clicked.

If I'm not explaining myself well, here's a graphical representation of what I'm trying to achieve:

enter image description here

I know that I can use something like this to pick random items from an array:

var myArray = [
  "Apples",
  "Bananas",
  "Pears"
];

var randomItem = myArray[Math.floor(Math.random()*myArray.length)];

But I don't know how I will organise the arrays and create the rules so that the result follows routes as displayed in my graphic above.

I imagine it would go something like this for result A, for example:

  • User clicks button
  • Script randomly chooses between Alliance and Horde (Picks Alliance)
  • Script then randomly chooses between Hunter and Paladin (Picks Paladin)
  • Script then randomly chooses between Human and Dwarf (Picks Human)
  • Script then randomly chooses between Holy, Protection and Retri (Picks Protection)
  • Finally script prints the whole result in HTML

Is this possible in jQuery? Could someone help me achieve this? Thanks!




Aucun commentaire:

Enregistrer un commentaire