From 3a82e9f8571d229c730e74df749136c222cafaa2 Mon Sep 17 00:00:00 2001 From: Ashraf Nazar Date: Wed, 22 Apr 2020 17:20:30 +0100 Subject: [PATCH] fix: add more examples on how to set key-value pairs (#38472) * fix(Curriculum): update examples on how to set key-value pairs * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-data-structures/add-key-value-pairs-to-javascript-objects.english.md Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-data-structures/add-key-value-pairs-to-javascript-objects.english.md Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * Update add-key-value-pairs-to-javascript-objects.english.md * Update add-key-value-pairs-to-javascript-objects.english.md Co-authored-by: Tom <20648924+moT01@users.noreply.github.com> --- ...lue-pairs-to-javascript-objects.english.md | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-data-structures/add-key-value-pairs-to-javascript-objects.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-data-structures/add-key-value-pairs-to-javascript-objects.english.md index 6d141e4e87..0530a9e0b7 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-data-structures/add-key-value-pairs-to-javascript-objects.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-data-structures/add-key-value-pairs-to-javascript-objects.english.md @@ -7,37 +7,54 @@ forumTopicId: 301153 ## Description
-At their most basic, objects are just collections of key-value pairs, or in other words, pieces of data mapped to unique identifiers that we call properties or keys. Let's take a look at a very simple example: +At their most basic, objects are just collections of key-value pairs. In other words, they are pieces of data (values) mapped to unique identifiers called properties (keys). Take a look at an example: ```js -let FCC_User = { - username: 'awesome_coder', - followers: 572, - points: 1741, - completedProjects: 15 +const tekkenCharacter = { + player: 'Hwoarang', + fightingStyle: 'Tae Kwon Doe', + human: true }; ``` -The above code defines an object called FCC_User that has four properties, each of which map to a specific value. If we wanted to know the number of followers FCC_User has, we can access that property by writing: +The above code defines a Tekken video game character object called tekkenCharacter. It has three properties, each of which map to a specific value. If you want to add an additional property, such as "origin", it can be done by assigning origin to the object: ```js -let userData = FCC_User.followers; -// userData equals 572 +tekkenCharacter.origin = 'South Korea'; ``` -This is called dot notation. Alternatively, we can also access the property with brackets, like so: +This uses dot notation. If you were to observe the tekkenCharacter object, it will now include the origin property. Hwoarang also had distinct orange hair. You can add this property with bracket notation by doing: ```js -let userData = FCC_User['followers']; -// userData equals 572 +tekkenCharacter['hair color'] = 'dyed orange'; +``` + +Bracket notation is required if your property has a space in it or if you want to use a variable to name the property. In the above case, the property is enclosed in quotes to denote it as a string and will be added exactly as shown. Without quotes, it will be evaluated as a variable and the name of the property will be whatever value the variable is. Here's an example with a variable: + +```js +const eyes = 'eye color'; + +tekkenCharacter[eyes] = 'brown'; +``` + +After adding all the examples, the object will look like this: + +```js +{ + player: 'Hwoarang', + fightingStyle: 'Tae Kwon Doe', + human: true, + origin: 'South Korea', + 'hair color': 'dyed orange', + 'eye color': 'brown' +}; ``` -Notice that with bracket notation, we enclosed followers in quotes. This is because the brackets actually allow us to pass a variable in to be evaluated as a property name (hint: keep this in mind for later!). Had we passed followers in without the quotes, the JavaScript engine would have attempted to evaluate it as a variable, and a ReferenceError: followers is not defined would have been thrown.
## Instructions
-Using the same syntax, we can also add new key-value pairs to objects. We've created a foods object with three entries. Add three more entries: bananas with a value of 13, grapes with a value of 35, and strawberries with a value of 27. + A foods object has been created with three entries. Using the syntax of your choice, add three more entries to it: bananas with a value of 13, grapes with a value of 35, and strawberries with a value of 27.
## Tests