2

I'm a complete beginner trying to learn Javascript. I am trying to complete a problem requiring me to return a value assigned to a key. The function is called getProperty and I am stuck - it keeps returning "should return the value of the property located in the object at the passed in key" every time I attempt to run a test on the code.

    var obj = {key: 'value'};

    function getProperty(obj, key) {
        var o = obj.key;
        return(o);
    }

    console.log(getProperty);
2
  • please make your description really short what is actually neded to answer the question. most of the people skips if there is a long description. Commented Sep 13, 2017 at 20:38
  • point taken into consideration for the next time I have a question Commented Sep 13, 2017 at 21:21

2 Answers 2

5

For dynamic attribute names you need to use the bracket [] notation instead of the dot notation:

var o = obj[key];
return o

Thanks to @Gaby for pointing out that you also need to call the function with valid arguments:

console.log(getProperty(obj, 'key'));
Sign up to request clarification or add additional context in comments.

1 Comment

and to log it they need to actually call the method console.log(getProperty(obj,'key'));
1

The "key" parameter for the method is irrelevant since you are not using it anywhere in the method. Also, the method needs the obj parameter when you call it.

   var obj = {key: 'value'};

    function getProperty(obj) {
        var o = obj.key;
        return(o);
    }

    console.log(getProperty(obj));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.