A script that deletes a random unequipped armor or weapon

Good afternoon.

I hope you're all well.
I'd like a script for RMMV that you might be able to help me with.

I would like a script that deletes a random unequipped armor or weapon every time a variable A reaches a value of 50. I would like this script to run a common event at the end, with a text with the armor's or weapon's name. The name is stored in another variable B. At the end, the script resets variable A.

Thanks to anyone who can help me.
JavaScript:
var inventory = $gameParty.equipItems();
var index = Math.randomInt(inventory.length);
var item = inventory[index];
if (item) {
  $gameParty.loseItem(item, 1);
  $gameVariables.setValue(40, item.name);
}
Replace 40 with the ID of your "variable B".
Spoiler: event example
◆If:#0039 Counter ≥ 50 ◆Control Variables:#0039 Counter = 0 ◆Control Variables:#0040 Item Name = 0 ◆Script:var inventory = $gameParty.equipItems(); : :var index = Math.randomInt(inventory.length); : :var item = inventory[index]; : :if (item) { : : $gameParty.loseItem(item, 1); : : $gameVariables.setValue(40, item.name); : :} ◆If:#0040 Item Name ≠ 0 ◆Text:None, Window, Bottom : :Lost 1x \v[40]. ◆ :End ◆ :End
[Edit: replaced allItems with equipItems, as rpgLord pointed out.]
Doesn't that check for all items? Shouldn't you check the equipItems (and maybe even filter away the accessories with etypeId)?
Doesn't that check for all items?
That's a good point.

Shouldn't you...even filter away the accessories with etypeId)?
Not specified by the OP...accessories are just a name for an armor slot, like helmet and shield.
Oops, thanks for pointing that out! I've edited my previous post to use equipItems instead.
Thank you all! Problem solved!

Original thread: https://forums.rpgmakerweb.com/threads/a-script-that-deletes-a-random-unequipped-armor-or-weapon.162006/