5. Who's in your bracket?


#1

I'm having an issue with this part. The console prints the job property but it gives me an error message "Oops, try again.
Do you have the variable 'aProperty' between brackets?" so I'm not sure why it isn't working. Can someone clarify this for me a little? Heres my code.

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);


#2

You will be getting 'programmer' from james["job"], so will throw a ReferenceError since there is no property name, 'programmer'.

aProperty = "job"

Now james[aProperty] will be programmer.


#4

Hi,
doing exactly as you said and it keeps telling me :
slightly_smiling:Oops, try again. It looks like james' job was not logged to the console.
A problem from code academy?


#5

post your code, and lets see what the problem is :slightly_smiling:


#6

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"
console.log(james[aProperty]);


#7

works fine for me, copy your code then try refreshing your browser, or clearing caches by pressing ctrl f5 paste your code back in and see if it lets you pass.


#8

Thx works now after refreshing the page.


#9

Hum. Thanks. I think the exercice is not very clear.


#10

OMG... Thank YOU.

The forums are my last resort. But I was going crazy.

This is really bad that you need to refresh the page for the code to work.

Learning JS is hard enough debugging your own code without having to second guess if the site is working correctly...


#11

works fine after refreshing the page.....

codeacademy needs to resolve this problem as well.


#12

Just looking for some clarification on why this aProperty = "job" works in this manner and is not just read as a variable with a string of "job"? There must be something I missed along the way.


#13

When accessing a property with subscript notation, we have to quote the strings:

james['job']

We can assign the string to a variable, in this case, aProperty and use the variable in the subscript:

aProperty = "job";
james[aProperty]

We would normally use dot notation when accessing properties directly; however, in a for..in statement we use a key iterator variable, so must use subscript notation.

for (var key in james) {
    console.log(key, james[key]);
}

Making sense?


#15

+1 on the tutorial being confusing and the super annoyance of having to refresh the page


#16

Glad you got through. It's thanks to the hard work you put forward.