I have been searching for an answer to my question, but so far I haven't found anything. I am self-taught and have encountered a gap in my knowledge that is puzzling me.
In a complex setup of connected pieces, I have two globally-scoped variables (essentially static) and an array of character types outside the main onclick function:
var missingWut = ["child","spouse","talisman","relic","sock"];
var rdmmissingWut;
var pronounA = "he";
var charTypes = [
["goatherd",pronounA+" wants to find a missing goat","kind"],
["shepherd",pronounA+" wants to find a missing sheep","cruel"],
["detective",pronounA+" wants to find a missing "+missingWut[rdmmissingWut],"spidery"],
...,
..., //this goes on for awhile; the array is currently 500 items long and has way more subindexes than I wanted/needed to include in this example.
];
The issue arises with the undefined variable rdmmissingWut at this point. To resolve this, we define rdmmissingWut inside the function, updating its value from undefined to a random index number:
rdmmissingWut = Math.floor(Math.random()*missingWut.length);
rdmcharType = Math.floor(Math.random()*charTypes.length);
We then assign a randomly chosen charType index to character 1 (char1).
var char1 = charTypes[rdmcharType];
My question is - Is there a way to update the variable value within the array without redefining the entire array after updating the variables?
One solution could be to redefine the array, which would update all variable values to their current values. However, this method seems cumbersome, cluttered, and inefficient.
Another scenario (with the same problem):
I intend to use the charTypes array to randomly select a character type for character 2 (char2) - and eventually for char3 & char4 as well. Suppose char2 (or 3 or 4) is female. After defining char1, I would need to change the value of pronounA to "she" and update all instances of it within the charTypes array before selecting a random charTypes index for her - am I correct? What would be the best approach to achieve this? Surely, there must be an elegant solution that I am unaware of.
Thank you for your assistance.