const randomEl = items[Math.floor(Math.random() * items.length)];
So if I understand you I would doIf you need me to explain this snippet, ask and I will let you know.Code:const randomEl = items[Math.floor(Math.random() * items.length)];
Also using let is better in JS if you are not going to use this variable in another code snipper as var has global scope and mess up other variables, if you are not sure which one to use and you dont have a lot of variables var is fine.
If the value wont change during runtime just use const
You are a life saver this worked perfectly for the random reward I was working on. Thank you.If you need me to explain this snippet, ask and I will let you know.Code:const randomEl = items[Math.floor(Math.random() * items.length)];
Also using let is better in JS if you are not going to use this variable in another code snipper as var has global scope and mess up other variables, if you are not sure which one to use and you dont have a lot of variables var is fine.
If the value wont change during runtime just use const
Yes that's right, for brevity's sake you can also use it in a single line and avoid declaring the random variable as below!So if I understand you I would do
let items =[1,2,3,4,5];
const randomEl = items[Math.floor(Math.random() * items.length)];
$gameParty.gainItem($dataItems[randomEl],1);
to reward the random item from the list. Am I understanding this correctly?
let items =[1,2,3,4,5];
$gameParty.gainItem($dataItems[items[Math.floor(Math.random() * items.length)]],1);
You're welcome, glad I could help!You are a life saver this worked perfectly for the random reward I was working on. Thank you.