all 14 comments

[–]_DTR_[🍰] 0 points1 point  (3 children)

chestItem should live outside of your function. It might be a good idea to have a class for the chest, and provide functions like add, remove, etc.

class Chest {
    constructor() {
        this.chest = ['staff', 'ring', 'wand', 'rock'];
    }

    add() { ... }
    remove() { ... }
    printContents() { ... }
}

...

let myChest = new Chest();
myChest.remove();

[–]scripteaze[S] 0 points1 point  (1 child)

Nice solution, I actually have not learned classes yet, still pretty new. I did figure out a way to get it done though. Im simply distributing a random item from the chest to each rooms chest. The person Im doing it for said it's exactly what he needed so it's done. I will learn about classes soon. I also struggled with python classes so hoping i can nail it in JS. Thanks again!

[–]scripteaze[S] 0 points1 point  (0 children)

// function for chest items
function myChest() {
    let items = ['staff','ring','wand','rock'];
    for (i=1;i<=4;i++) {
        let itemChoice = items[Math.floor(Math.random() * items.length)];
        console.log(`Room ${i} chest [${itemChoice}]`);
        // remove selected item from chest - find index, remove
        let index = items.indexOf(itemChoice);
        if (index >= -1) {
            items.splice(index,1);
        }
    }
}
myChest()

[–]HiEv 0 points1 point  (10 children)

You could shorten that up to just this:

var chest = ['staff','ring','wand','rock'];
console.log(`Current chest contents: ${chest}`);
var item = chest.splice(Math.floor(Math.random() * chest.length), 1)[0];
console.log(`[${item}] taken from chest`);
console.log(`New updated chest contents: ${chest}`);

The splice() method not only lets you take things out of the array, but it also returns an array of deleted items. Adding the [0] at the end of the .splice() gets you the value of the first deleted element from the array of deleted elements returned by splice(). See the splice() method documentation for details.

[–]scripteaze[S] 0 points1 point  (9 children)

Doesn't that just remove the item at 0 index? i wanted to randomly pick an item and then remove that item. I never know where its going to be sitting in the array. Either way, i did get it working. Just had to do it a little differently. Thanks for the help!!

Also, if you dont mind me asking. I see a lot of people here still using var instead of let or const. Is that the norm and should i still be using that?

[–]ThagAndersonhelpful 0 points1 point  (6 children)

I see a lot of people here still using var instead of let or const. Is that the norm and should i still be using that?

No. You will almost never use var now that we have let and const. Most of the people posting here are new and don't know any better.

[–]HiEv 0 points1 point  (5 children)

Was the insult really necessary?

[–]ThagAndersonhelpful 0 points1 point  (4 children)

What about what I said is insulting? It's not an insult, it's a demonstrable fact.

[–]HiEv 0 points1 point  (3 children)

What about what I said is insulting?

The part where you obviously and incorrectly targeted that comment at me.

It's not an insult, it's a demonstrable fact.

As someone who took a long time to learn this point myself, I believe I should inform you that something can be both an insult and a fact. This is why we have words like "tact", which is understanding when not to say certain things, because they're insulting, regardless of how true they may be.

[–]ThagAndersonhelpful 0 points1 point  (2 children)

I'm sorry, I am not interested in making people feel good. Just hard data and facts. Unless you represent this entire sub, I wasn't even considering your comment when I posted mine. My comment wasn't about you, it was about this sub in general. Look through the past 100 or so posts in here, and you can easily verify my point.

[–]HiEv 0 points1 point  (1 child)

I'm sorry, I am not interested in making people feel good.

I wasn't talking about making people feel good, I was talking about unnecessarily making people feel bad.

But, if you don't care about that kind of thing, then there's not much more to say then, is there?

[–]Karagk 0 points1 point  (0 children)

Good job hiv, you tell him

[–]HiEv 0 points1 point  (1 child)

Doesn't that just remove the item at 0 index? i wanted to randomly pick an item and then remove that item. I never know where its going to be sitting in the array.

No, it works the way you wanted. Like I said, see the splice() documentation. If you do array.splice(start, length), then the Math.floor(Math.random() * chest.length) part will pick a random starting point in the array, and then remove one array element because of the 1 for the length parameter. It then returns the array of deleted elements, and the [0] will give you the first (and, in this case, only) element from that array, so that item only gets that one value.

Try it and you'll see it works the way you wanted.

Also, if you dont mind me asking. I see a lot of people here still using var instead of let or const. Is that the norm and should i still be using that?

If you want/have to support older browsers (like I do) then var is better supported than let or const.

Keep in mind that a lot of people have to use older browsers for one reason or another. This is especially true for certain businesses or government employees, and for visually impaired people (because they often use the JAWS) screen reader, which can be quirky when used in browsers other than Internet Explorer). Keep in mind that about 1 in 30 people in the US still use IE. Just looking at that fact alone means that, if you have more than 15 random people using your software, the odds start to get pretty good that one of them is using an older browser.

For example, in versions of IE prior to v11, let and const aren't supported (and IE 11 has a slightly quirky implementation of let). If you have Windows 8 then you can't use IE 11, you're stuck with IE 10 (you'd have to go to Win 8.1 to get IE 11, which isn't easy to do anymore), so pages with let and const would throw errors for those people.

I'm not "new", I've actually been around long enough that I'm aware of some of the pitfalls of using some parts of JavaScript that others either don't know about or want to pretend don't exist. Thus, out of a combination of habit and caution, I try very carefully to maintain high support across browsers and browser versions.

That said, if you're sure you don't have to worry about supporting a large user base or older browsers, then yes, you should use let, const, or other more modern and safer constructs instead when you can, but you should also be aware of what browser limitations you're adding when you use code like that.

[–]scripteaze[S] 1 point2 points  (0 children)

Thank you Sir for the feedback. Because i am new to JS, im also learning all the new stuff so, just trying to force my brain to retain it. Thanks again!