Lesson 5 : Who's in Your Bracket?


#1

whats wrong with this code plz

var james = {
job: "programmer",
married: false
};

// set to the first property name of "james"
var aProperty = james["job"];

// print the value of the first property of "james"
// using the variable "aProperty"

console.log(aProperty);

error Oops, try again. Do you have the variable 'aProperty' between brackets?


#2

Runs fine for me.

Reset the exercise and refresh the browser, sometimes helps.


#3

The above will be the value, 'programmer', which is not a property in the james object.

var aProperty = 'job';

Now we have a property which we can query in subscript (bracket) notation:

console.log(james[aProperty]);   // programmer

#4

@mtf
using ur idea means i set the variable to get the string ' job' which brings put the word "job" instead of "programmer" at least with me,, plus giving same error message about brackets :frowning:


#5

Please re-post your code in a new Reply. Thanks.


#6

var james = {
job: "programmer",
married: false
};

// set to the first property name of "james"
var aProperty =james["job"];

// print the value of the first property of "james"
// using the variable "aProperty"

console.log(aProperty);


#7

i did

but still the same :frowning:


#8

You are still logging the variable, rather than the value:

console.log(aProperty)

should be,

    console.log( james[aProperty] );

#9

unfortunately
console.log(james[aProperty]);
gives a syntax error when james["job"];
or job is undefined if james[job];


#10

I also happen fine. Thanks! :wink:


#11

this is simply my code
var james = {
job: "programmer",
married: false
};
var aProperty = james['job'];
console.log(aProperty);

it prints out "programmer"as instructed
butr saying did u put aProperty in brackets?

y?

i cant believe such a short (3 lins ) code making all thistrouble for me
what about big programs? :persevere::cry:


#12

Start with a fresh slate. Wipe it clean. Now read the lesson, and the instructions, thoroughly, all the way through before writing any code. See the objective and the big picture? Now take a breath, and write...


#13

finally worked

you were right
var aProperty = "job";
console.log(james[aProperty]);
worked,

dont know if there is a problem with htis site!!!?
coz i believe i have tried the same before , or may be im wrong though,

thank you very much


#15

Ok, I had some trouble with this exercise as well, but after reading this, and reading the exercise again, I have completed it.
But now my question is, why would you do that? Why not just write console.log(james["job"]) instead of storing it in a variable? Wouldn't that be faster?


#16

It's possible to extend this lesson once we understand the mechanics. Knowing that we can access a property value using a variable to reference its key, we can iterate over an object like an array...

for (var key in object) {
    console.log(key);          // each key in object
    console.log(object[key]);  // value associated with key
}

Here we use a variable to access each property of the object and examine each key and associated value. We cannot do something dynamic like this with dot notation.


#17

var james = {
job: "programmer",
married: false
};

// set to the first property name of "james"
var aProperty = "job";

// print the value of the first property of "james"
// using the variable "aProperty"
james[aProperty];

this is my code and I still have an error . on the console appears "programmer" but say it is not


#18

It's okay now. but i didn't understand so well.


#19

ANOTHER is solution is to reload the browser when you are sure that your code is right.


Yep that worked
#20

I don't understand why setting var aProperty = "job" works. Why does that not just create a variable set to the string "job"? How does it know "job" refers to a property from the object?


#21

"job" is just a string. aProperty is assigned a reference to the string object in memory. When we query the variable, we are actually querying the object it points to.

console.log(aProperty, typeof aProperty);     // job string

Property names (keys) are also strings. That's why when we use subscript (bracket) notation to reference a property we have to enclose it in quotes.

console.log(james['job']);                    // programmer

We should note that it is not a variable named job but a property specific name that can only be referenced as a member of the parent object. james.job and james['job'].

A variable is not a variable if it is in quotes. That makes it a string. But since it references a string, in our case, it can be used to represent that property name:

console.log(james[aProperty]);                // programmer