* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
88 lines
1.4 KiB
Markdown
88 lines
1.4 KiB
Markdown
---
|
|
id: 56bbb991ad1ed5201cd392d2
|
|
title: Add New Properties to a JavaScript Object
|
|
challengeType: 1
|
|
videoUrl: 'https://scrimba.com/c/cQe38UD'
|
|
forumTopicId: 301169
|
|
dashedName: add-new-properties-to-a-javascript-object
|
|
---
|
|
|
|
# --description--
|
|
|
|
You can add new properties to existing JavaScript objects the same way you would modify them.
|
|
|
|
Here's how we would add a `"bark"` property to `ourDog`:
|
|
|
|
`ourDog.bark = "bow-wow";`
|
|
|
|
or
|
|
|
|
`ourDog["bark"] = "bow-wow";`
|
|
|
|
Now when we evaluate `ourDog.bark`, we'll get his bark, "bow-wow".
|
|
|
|
Example:
|
|
|
|
```js
|
|
var ourDog = {
|
|
"name": "Camper",
|
|
"legs": 4,
|
|
"tails": 1,
|
|
"friends": ["everything!"]
|
|
};
|
|
|
|
ourDog.bark = "bow-wow";
|
|
```
|
|
|
|
# --instructions--
|
|
|
|
Add a `"bark"` property to `myDog` and set it to a dog sound, such as "woof". You may use either dot or bracket notation.
|
|
|
|
# --hints--
|
|
|
|
You should add the property `"bark"` to `myDog`.
|
|
|
|
```js
|
|
assert(myDog.bark !== undefined);
|
|
```
|
|
|
|
You should not add `"bark"` to the setup section.
|
|
|
|
```js
|
|
assert(!/bark[^\n]:/.test(code));
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --after-user-code--
|
|
|
|
```js
|
|
(function(z){return z;})(myDog);
|
|
```
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
// Setup
|
|
var myDog = {
|
|
"name": "Happy Coder",
|
|
"legs": 4,
|
|
"tails": 1,
|
|
"friends": ["freeCodeCamp Campers"]
|
|
};
|
|
|
|
// Only change code below this line
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
var myDog = {
|
|
"name": "Happy Coder",
|
|
"legs": 4,
|
|
"tails": 1,
|
|
"friends": ["freeCodeCamp Campers"]
|
|
};
|
|
myDog.bark = "Woof Woof";
|
|
```
|