Probably a super simple question. But on slide 18 on the Unit 5 slideshow, I am not getting the answer shown on the slide. My issue is with Line 2 of the program. Can someone explain how they got that answer? TIA.
In Javascript [ ] evaluates the expression inside them to get the name of a property stored in the referenced object. So, 2 - 1 evaluates to 1. So myStuff[1] (or myStuff.1) is assigned the value “tree”.
Thank you for your quick response. But I was actually confused with the next line of the program (02). My questions wasn’t clear - when I said line 2 I didn’t mean the 2nd line I meant 02 which is the 3rd line of the program. myStuff[myStuff[2]] = myStuff[0];
If you could clarify that one, I would realy appreciate it!
Okay, that one bends the mind a bit.
Let’s look at evaluating that the way Javascript would. We evaluate myStuff[0]
to be “dog”. Then we look for where to assign it.
We look to evaluate myStuff[myStuff[2]]
we see [ ] so we need to evaluate myStuff[2]
. Because there are more [ ] we evaluate the 2
inside them. Well, that is just 2. So now we can resolve myStuff[2]
to be 3 from the array. Having that we now have myStuff[3]
which currently holds the value 10.
So we assign the value “dog” to the location myStuff[3]
, the end of the list.
Thank you so much! That makes sense now.
Yes, thank you, I was struggling with that one.