should give an error when you attempt to run this code. Because appetizers is now a undefined variable.
we can only help you, in the end, you will have to get to the finish. To achieve this, its important to teach yourself thinking steps like: what do i try to do here? How can achieve this? Which methods do i have to achieve this?
I don’t know how to say this nicely, but your reply perfectly illustrate the problem.
but why do we use brackets? Your steps behind. Yes, we have to use brackets (the associative array notation). But its important to understand why
so, objects and associative arrays are the same thing in JS. But this leaves us with two notation to access a property:
obj.prop // object notation
or:
obj['prop'] // associative array notation
in this exercise we can’t use the object notation because we want to pass the property/key as argument to a method. Which means we have to use the associative array notation.
I made quite a leap there, didn’t I?
if you try to pass the property:
this.getRandomDishFromCourse(appetizers);
you get an undefined variable, which is why we can’t use object notation.
I understand the difference between dot notation and bracket notation… I don’t understand why we pass a string into bracket notation… If you pass a variable in, it would just give you the key right? Does appetizers return undefined because it is an empty array?
With bracket notation you can also use a variable inside the brackets to select the keys of an object. So, passing in appetizers (without ‘’) passes it as a key, and doesn’t give us a value.
we get the value, appetizers is the key, we use the key to do a lookup, resulting in the value
why would it go from one data type (array) to another data type (undefined)?
a variable has a value (for example a string), like in my example:
theKey = 'myKey'
which we can use as key to lookup a value in the object using the associative array notation.
In the meal maker project, you pass 'appetizers' string as argument to courseName parameter, so now we have a variable (courseName) with a string value (’appetizers’`) which we can use as key to get value from the object
It is called associate array notation because you have to declare strings when accessing an element in an array. For instance,
var cars = [“Saab”, “Volvo”, “BMW”];
We can access “Volvo” with cars[1]… 1 is a number data type, but with objects, we use strings like menu._courses[“appetizers”]. “appetizers” is a string data type. Does this mean that objects are not all indexed like arrays. Even though all arrays are objects but not all objects are arrays?