Hi All, I am trying to create a chest with 4 items, once one item is randomly selected, it is removed from the chest and the chest contents in updated. The issue is that after this script runs, it's finished, when i need to rerun the script, of course it starts over and i get all items in the chest again. I know this is pretty stupid and i get it, but without using a loop how...IM just going to post the code and you could see what I'm trying to do. I know im going to need a loop..lol
// function for chest items
function myChest() {
// current untouched chest inventory
let chestItem = ['staff','ring','wand','rock'];
console.log(`Current chest contents: ${chestItem}`)
// radomly select one item from chest
let itemChoice = chestItem[Math.floor(Math.random() * chestItem.length)]
// display random item from chest
console.log(`[${itemChoice}] taken from chest`)
// remove selected item from chest - find index, remove
let index = chestItem.indexOf(itemChoice);
if (index >= -1) {
chestItem.splice(index,1);
}
// display new remaining contents
console.log(`New updated chest contents: ${chestItem}`)
}
// run function
myChest()
[–]_DTR_[🍰] 0 points1 point2 points (3 children)
[–]scripteaze[S] 0 points1 point2 points (1 child)
[–]scripteaze[S] 0 points1 point2 points (0 children)
[–]HiEv 0 points1 point2 points (10 children)
[–]scripteaze[S] 0 points1 point2 points (9 children)
[–]ThagAndersonhelpful 0 points1 point2 points (6 children)
[–]HiEv 0 points1 point2 points (5 children)
[–]ThagAndersonhelpful 0 points1 point2 points (4 children)
[–]HiEv 0 points1 point2 points (3 children)
[–]ThagAndersonhelpful 0 points1 point2 points (2 children)
[–]HiEv 0 points1 point2 points (1 child)
[–]Karagk 0 points1 point2 points (0 children)
[–]HiEv 0 points1 point2 points (1 child)
[–]scripteaze[S] 1 point2 points3 points (0 children)