Who's in Your Bracket? - my code seems to work, but the editor doesn't like it


#1



As far as I can tell, my code does what it is supposed to, but I keep failing the exercise. Has anyone else experienced anything similar?

When I run it, the console prints the word "programmer" as it should, but I get an "Oops, try again. Do you have the variable 'aProperty' between brackets?"


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

Heya, your code actually works perfectly fine in regards to it's functionality! The editor just wants you to set the "aProperty" variable as a string, which you then place in brackets, as shown below.

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

The primary reason you would save the variable as a string instead of as the property itself is for the sake of reuse. If you had two, or three, or twenty objects, it's easier to have a variable that each object could reference, as opposed to making a new variable for each object. For example:

var james = {
    job: "programmer",
    married: false
};
var tom = {
    job: "writer",
    married: false
};
var aProperty = 'job';
console.log(tom[aProperty]);
console.log(james[aProperty]);
// will return 'writer' and 'programmer' respectively

Your code is good though, it's just not what the codecademy parser was looking for!


Who's in Your Bracket? - Introduction to Objects II
#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.