.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
-iterate-through-the-keys-of-an-object-with-a-for...in-statement.english.md
access-an-arrays-contents-using-bracket-notation.english.md
access-property-names-with-bracket-notation.english.md
add-items-to-an-array-with-push-and-unshift.english.md
add-items-using-splice.english.md
add-key-value-pairs-to-javascript-objects.english.md
check-for-the-presence-of-an-element-with-indexof.english.md
check-if-an-object-has-a-property.english.md
combine-arrays-with-the-spread-operator.english.md
copy-an-array-with-the-spread-operator.english.md
copy-array-items-using-slice.english.md
create-complex-multi-dimensional-arrays.english.md
generate-an-array-of-all-object-keys-with-object.keys.english.md
iterate-through-all-an-arrays-items-using-for-loops.english.md
modify-an-array-stored-in-an-object.english.md
modify-an-object-nested-within-an-object.english.md
remove-items-from-an-array-with-pop-and-shift.english.md
remove-items-using-splice.english.md
use-an-array-to-store-a-collection-of-data.english.md
use-the-delete-keyword-to-remove-object-properties.english.md
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
formattingConversion
math-challenges
requiresTests
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
guide
mock-guide
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.french.md
README.italian.md
README.md
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env
* fix(curriculum): tests quotes * fix(curriculum): fill seed-teardown * fix(curriculum): fix tests and remove unneeded seed-teardown
3.5 KiB
3.5 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
587d7b7c367417b2b2512b18 | Add Key-Value Pairs to JavaScript Objects | 1 |
Description
let FCC_User = {The above code defines an object called
username: 'awesome_coder',
followers: 572,
points: 1741,
completedProjects: 15
};
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:
let userData = FCC_User.followers;This is called dot notation. Alternatively, we can also access the property with brackets, like so:
// userData equals 572
let userData = FCC_User['followers']Notice that with bracket notation, we enclosed
// userData equals 572
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
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
.
Tests
tests:
- text: <code>foods</code> is an object
testString: assert(typeof foods === 'object', '<code>foods</code> is an object');
- text: The <code>foods</code> object has a key <code>"bananas"</code> with a value of <code>13</code>
testString: assert(foods.bananas === 13, 'The <code>foods</code> object has a key <code>"bananas"</code> with a value of <code>13</code>');
- text: The <code>foods</code> object has a key <code>"grapes"</code> with a value of <code>35</code>
testString: assert(foods.grapes === 35, 'The <code>foods</code> object has a key <code>"grapes"</code> with a value of <code>35</code>');
- text: The <code>foods</code> object has a key <code>"strawberries"</code> with a value of <code>27</code>
testString: assert(foods.strawberries === 27, 'The <code>foods</code> object has a key <code>"strawberries"</code> with a value of <code>27</code>');
- text: The key-value pairs should be set using dot or bracket notation
testString: assert(code.search(/bananas:/) === -1 && code.search(/grapes:/) === -1 && code.search(/strawberries:/) === -1, 'The key-value pairs should be set using dot or bracket notation');
Challenge Seed
let foods = {
apples: 25,
oranges: 32,
plums: 28
};
// change code below this line
// change code above this line
console.log(foods);
Solution
// solution required