Meal Project

Why do I have to put appetizers as ‘appetizers’ ? Is it because it is accessing an item in the array?

Why wouldn’t I just write appetizers without the apostrophes… Wouldn’t that allow it to loop through the previous function getRandomDishFromCourse?

I thought i explained the concept of using a string to access an element of an object/associative array using the associative array notation?

Yes, but I don’t really understand.

array and associative array are two very different things. Did you understand that?

how would this code look like then? Go ahead and try this code, see what issues you run into, that should help your understanding

const appetizer = this.getRandomDishFromCourse(appetizers);

It’s because appetizers is a string?

this:

 this.getRandomDishFromCourse(appetizers);

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?

When you put ‘appetizers’ in the getRandomDishFromCourse function, it has to be a string because it’ll be in brackets in the function.

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.

good, you start using the correct terms :slight_smile: Now we are getting somewhere.

simple enough to test, right? You could write a code sample to test this theory. Something like this:

myObj = {
  myKey: 3
}

theKey = 'myKey'
console.log(myObj[theKey]);

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

1 Like

Yay,
I think I get it.

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?

const menu = {
_courses: {
appetizers: ,
mains: ,
desserts:
},

Arrays are objects… which is why they’re similar

i already covered that here:

the relevant bit:

Javascript has arrays:

myArray = [1, 2, 3]

objects:

myObj = {a: 1, b: 2}

and associative arrays:

myAssoc =  {a: 1, b: 2}

and i think i mentioned it somewhere again, that arrays and associative arrays are not the same thing.

array are containers, they can hold data. Objects and associative arrays can also manipulate this data (using methods)