Frisky Orangutan said:
I’m new to Nightbot and greatly appreciate information on the language.
Nightbot uses JavaScript inside the $(eval) variable, with specific variables here and there occasionally, nothing too extravagant.
Frisky Orangutan said:
I’m actually using this code twice in the same command, once for a random question and once for a random answer. Both sets of prompts are separated within the same pastebin, which is why I’m currently using a set number.
Then use the .length property and divide it by two? I'm trying to help you write code that you won't need to update every time you edit your Pastebin, which is how ideal code should work, it has to be flexible.
p[Math.floor(Math.random() * Math.floor(p.length / 2))]
Frisky Orangutan said:
I intend to use a Math.random() * (second part length) + (first part length) for the answer section of the command.
That'd work if all the answers are at the end of the Pastebin, I have to assume it's the case given you haven't shared it.
p[Math.floor(Math.random() * Math.floor(p.length / 2)) + Math.floor(p.length / 2)]
The advantage of the two last bits of code is that even if you add a semicolon to the last entry of your Pastebin, this won't create an issue, contrary to what I said before, and that's thanks to Math.floor() when dividing in half.
Clean code also respect the DRY principle, so if we notice an element that's repeating, you want to store it in a variable, or something else, with all that in mind, that's how I'd rewrite the code: