Who's in your bracket? Need help!


#1

Instructions

Take advantage of the ability to use variables with bracket notation.

In line 7, set aProperty to a string of the first property in james (ie. the job property).

Then print james’s job using bracket notation and aProperty.

Code

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

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

// print the value of the first property of “james”
// using the variable “aProperty”

Problem

I have no idea what to do. "Set to a string of the first property"? My issue isnt terms, or syntaxes - it's the instructions. Can somebody explain them to me?
This is the Link to exersice


#2

Hi @johncdf :smiley:
This one confused me a bit too, but what the instructions are asking you to do is set the first variable, aProperty, to the FIRST property in the james object.
And then use that variable for bracket notation: console.log(james["aProperty"]);

I hope this helps


#3

Here some explanation…

james job married explained

- - - B a s i c s - - -

Each Object has one or more properties.
Each property consists of a property-key and it’s associated value.

 var object1 = {
              name: "First"
              }

So object1 has 1 property with
a type property-key name
and it’s associated value “First” (which in this case is a “string-value”)
OR

var myObj = {
         type: 'fancy',
         disposition: 'sunny'
        }

myObj has 2 properties separated by a comma-,,
a property with property-key type and an associated-string-value 'fancy’
a disposition-property with property-key disposition and
…an associated-string-value ‘sunny’.

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

var james = {
    job: "programmer",
   married: false,
   sayJob: function() {
          // complete this method
          console.log("Hi, I work as a" + this.job);
    }
 };

Description of the james object.
The james object has 3 properties which are separated by a comma-,

there is a job property with property-key job and it’s associated string value 'programmer’
there is a married property with property-key married and it’s associated boolean value false
there is a sayJob property with property-key sayJob and it’s associated anonymous function VALUE
( they also would ‘say’, the james-object has the sayJob()-Method )

##access via dot-notation
(no variable allowed, only literal property-key-name )

james.job ==> you will get the associated string VALUE of the job property-key, thus
you get the string VALUE ‘programmer’

james.married ==> you will get the associated boolean VALUE false

james.sayJob ==> you will get the associated anonymous function VALUE
to call/execute this method you add a pair of parenthesis-( )
james.sayJob();

##access via the square-bracket-notation
###1 using the literal property-key
james[“job”]
james[“married”]
james[“sayJob”]()

###2 using the property-key by reference (=== via a variable )
var propertyKey1 = “job”;
var propertyKey2 = “married”;
var propertyKey3 = “sayJob”;
james[propertyKey1] ==> you will get the associated string VALUE of the job property-key, thus
you get the string “programmer”

james[propertyKey2]
in this case you will get the boolean VALUE of the married property
being false

james[propertyKey3]();
would =display= “Hi, I work as a programmer”


#4

Thank u so much. The question put up was confusing. U have made it simple while explaining


#5

As an extra…

An object has one or more properties, separated by a comma_,

A property has a property-key and it’s associated VALUE

The VALUE can be anything from:

•Number
•String
•Boolean
•Symbol
•Object  ◦Function/'Method'
         ◦Array
         ◦Date
         ◦RegExp
         ◦Math
•Null
•Undefined

=============================================
and on the lookout for knowledge

Reference

==
google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html <---JavaScript code-convention
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript
http://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function
http://stackoverflow.com/questions/6260475/how-to-the-operator
http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1


#6

Finally! It worked! Thanks for the explanation!:star_struck:


#7

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