lundi 24 décembre 2018

Js: How to pick a corresponding array entry given a randomly selected value

I'm trying to make a story generator for future playthroughs of some of my favourite games. To this end, I want to be able to randomly pick a combat style from an array (which I have succeeded in), but then also pick a corresponding explanation of that style to display in a paragraph.

I have:

function randomValueFromArray(array){
  return array[Math.floor(Math.random()*array.length)];
}
function result() {
    var jobItem = randomValueFromArray(insertJob);
    document.getElementById('jobDisplay').innerHTML = jobItem;
    var newStory = storyText;
    var jobexpItem = randomValueFromArray(insertJobexp),
        yItem = randomValueFromArray(insertY),
        zItem = randomValueFromArray(insertZ);
    newStory = newStory.replace(':insertJob:', jobItem);
    newStory = newStory.replace(':insertJobexp:', jobexpItem);
    newStory = newStory.replace(':inserty:', yItem);
    newStory = newStory.replace(':insertz:', zItem);
}

referring to my arrays, containing lists of potential characteristics.

The jobItem selected might be number 10 on a list of 30. I want to then select entry number 10 in the jobexpItem array, which is an explanation of the entry selected in jobItem. These are listed in different areas for simplicity - I have a table with only the basic descriptors, and a paragraph with explanations included (where the storyText is displayed).

At the moment, I have jobexpItem as a random value just for troubleshooting. How would I link jobexpItem's value to the corresponding random jobItem value chosen?




Aucun commentaire:

Enregistrer un commentaire