I've got my percentages hammered out along with my arrays. I know I need to make it so that the percentage determines WHICH array is picked, and then I need to shuffle that array to make it spit out one of the three "things". I know there's an easier/more efficient way to do this without clogging my code with a million shuffle functions to determine the "Thing" variable.
Currently, it's not working (spits out "undefined") but it's left my scratching my head as I'm not sure what the issue is, along with wanting to streamline it.
The entire point of the code is to pick an array based on the rolled percentage, randomize that array, and spit back out the value it got from shuffling it.
Current absolute dumpster-fire I'm working with:
function generate(){
var tierOne = ["thing one", "thing two", "thing three"]
var tierTwo = ["thing four", "thing five", "thing six"]
var tierThree = ["thing seven", "thing eight", "thing nine"]
var tierFour = ["thing ten", "thing eleven", "thing twelve"]
var tierFive = ["thing thirteen", "thing fourteen", "thing fifteen"]
var percent = r();
if (percent >= 0 && percent < 25) {
shuffle(tierOne)
thing = tierOne;
return thing[0];
} else if (percent >= 25 && percent < 36) {
shuffle(tierTwo)
thing = tierTwo;
return thing[0];
} else if (percent >= 36 && percent < 60) {
shuffle(tierThree)
thing = tierThree;
return thing[0];
} else if (percent >= 60 && percent < 76) {
shuffle(tierFour)
thing = tierFour;
return thing[0];
} else {
shuffle(tierFive)
thing = tierFive;
return thing[0];
}
}
function r() {
Math.floor(Math.random() * 100) + 1;
return Math.floor(Math.random() * 100) + 1;
}```
Your shuffle routine should probably return a new array with the results.
You need to declare thing
, and not use a global variable.
if (percent >= 0 && percent < 20) {
const thing = shuffle(tierOne)
return thing[0];
}
or
let thing
if (percent >= 0 && percent < 20) {
thing = shuffle(tierOne)
return thing[0];
}