Is there a better way to do this clunky function (maps, arrays, sets or none of them)?

4 days ago 2
ARTICLE AD BOX

For context: I'm new to coding and still learning.

I recently started to play around with JavaScript, and was trying to make this function as "unrepetitive" as possible. I'd like it so I could pick a random entry from an array (or something similar) that meets the logical comparison. In this case, if the type parameter is the same as the category in the entry.

You can see that I did somewhat achieve it like this, but it feels clunky... I'm not too sure. I haven't tested it just yet (kinda scared to see none of it works). So I'd appreciate constructive feedback!

function randStatus(type = "status") { let status = ""; const buffOptions = [ "gunslinger", "teamwork", "sharpness", "distraction", "trick", "undying", "badassery", "stability", ]; const debuffOptions = [ "fire", "fear", "grappled", "infection", "prone", "limping", "dizziness", "slumber", ]; if (type === "buff") { status = buffOptions[randNum(0, buffOptions.length)]; } else if (type === "debuff") { status = debuffOptions[randNum(0, debuffOptions.length)]; } return status; }

I have tried maps, but then I couldn't pick a random map entry. I also tried objects, but I'm unsure if it's a bit of an overkill to create an object for each entry (maybe I misunderstood how objects work). I don't fully understand sets either, so I haven't attempted them yet.

(dunno if it works, first time trying this...)

Read Entire Article