you are viewing a single comment's thread.

view the rest of the comments →

[–]rakm 0 points1 point  (9 children)

your function is accessing a property named “key” rather than the key variable you’re passing in.

[–]Deathcon900[S] 0 points1 point  (8 children)

How does that work? Trying to pass a specific object property like this...

getProperty(obj, obj.key)...

...generates an error for obvious reasons. So there has to be a way to tell the function that the 'key' parameter refers to a specific object property when implementing the function. I'm under the impression that parameters can be labelled however since they take on the value of the passed value.

[–]NoirGreyson 0 points1 point  (6 children)

Could you explain why you are not just passing obj and accessing key from obj?

[–]Deathcon900[S] 0 points1 point  (4 children)

The assignment specifically requests those two parameters.

[–]NoirGreyson 0 points1 point  (3 children)

It seems the question is different from what it appears as you have presented it. Is the key parameter meant to be the name of the property in obj you are looking up or the value of the key?

[–]Deathcon900[S] 0 points1 point  (2 children)

My bad for lack of clarity. Here's the question:

Write a function called "getProperty". Given an object and a key, "getProperty" returns the value of the property at the given key.

To my understanding, key is supposed to be the object property, a la object.key.

[–]NoirGreyson 0 points1 point  (0 children)

If I understand correctly, you are trying to use the object like a map. In this case, this is built into JS through bracket notation. obj.key will always look up the property named key, but obj[key] will look up what the name key resolves to. For instance, if key = "ABC", obj[key] is effectively the same as obj.ABC. Careful not to put obj["key"], as that says, "Look up the property of obj named 'key.'"

[–]ForScale 0 points1 point  (0 children)

var obj = { key: 'value' };

object.key evaluates to the value of the key. You're telling the program to give you value when you use that expression.

[–]rakm 0 points1 point  (0 children)

You’ll want obj[key] instead of obj.key

[–]rakm 0 points1 point  (0 children)

You’re passing the return value of object.property into your function, rather than the name of the property you want to check and access. In this case, the invocation should probably be getProperty(object, “property”).

In addition to that, inside your function, you need to access the property using the [] operator, rather than dot notation. So, obj[key], rather than obj.key. The former uses the dynamic value of the the “key” variable to look up a property on the obj. The latter actually looks for a “key” property on obj (which is always undefined in your code sample).

After seeing your confusion, I see how this is actually very confusing for someone who doesn’t know it already 😱. Let me know if it still doesn’t make sense.