Arrays and Objects - Practice Makes Perfet


#1



I think there is a bug in this exercise. I have the syntax EXACTLY as you would enter the object using an object literal notation and keep getting an 'syntax error' message.

https://www.codecademy.com/en/courses/javascript-beginner-en-9Sgpi/2/4?curriculum_id=506324b3a7dffd00020bf661#


I keep getting the following error message:
SyntaxError: Expected ':'


var object1 = {
    home: "Kentucky",
    college: "University of Louisville",
    degree: "History"
};

var object2 = {
    age: 77,
    nationality: "American",
    home: "New York"
};

var object3 = {
    favorite food: "pizza",
    favorite nonalcoholic beverage: "Coca-Cola",
    favorite alcoholic beverage: "Yuengling"
};


#2

@dtcarter84

var object1 = {
    home: "Kentucky",
    college: "University of Louisville",
    degree: "History"
};

var object2 = {
    age: 77,
    nationality: "American",
    home: "New York"
};

var object3 = {
    "favorite food": "pizza",
    "favorite nonalcoholic beverage": "Coca-Cola",
    "favorite alcoholic beverage": "Yuengling"
};
console.log(object1);
console.log(object2);
console.log(object3);
console.log(object3['favorite food']);

If you use spaces in your object property-key
you will have to encapsulate the combination in a pair of single of double-quotes....


#3

I did not know that we can write object3 that way. That's cool!


#4

Thank you! I did not realize that. I appreciate your help.


#5

I have another question for you. Instead of using quotations, could I also have eliminated the space and capitalized the second word, creating a variable, like "favoriteFood", "favoriteNonalcoholicBeverage", and "favoriteAlcoholicBeverage"?


#6

var object3 = {
"favoriteFood": "pizza",
"favorite nonalcoholic beverage": "Coca-Cola",
"favorite alcoholic beverage": "Yuengling"
};

console.log(object3['favoriteFood']);

As you asked, I tested it. It seems you can use this. You can even try yourself in codecademy Labs:link But I think JavaScript prefers dot notation.


#7

Thanks. I used the quotations too, and those worked.


#8

@dtcarter84,
It is the preferred way
to use the so-called =camel-Style= of writing variable-names or property-keys
like
productionPlantSecurityEntryProtocol
Read
http://javascript.crockford.com/code.html <---JavaScript code-convention

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


#9

They do not like white spaces unless it is strictly necessary as I learnt from the links. But what about the notation. which one is preferred, What's the difference if I write:

console.log(games.steam);

console.log(hackers["privacy"]) is it for readability or for performance? I did not find anywhere which one is preferred and why.


#10

@mashuk,

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 seperated 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

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"