2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: 56bbb991ad1ed5201cd392d2
|
|
|
|
title: Add New Properties to a JavaScript Object
|
|
|
|
challengeType: 1
|
2019-02-14 12:24:02 -05:00
|
|
|
videoUrl: 'https://scrimba.com/c/cQe38UD'
|
2019-08-05 09:17:33 -07:00
|
|
|
forumTopicId: 301169
|
2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
|
|
|
You can add new properties to existing JavaScript objects the same way you would modify them.
|
|
|
|
Here's how we would add a <code>"bark"</code> property to <code>ourDog</code>:
|
2018-10-08 01:01:53 +01:00
|
|
|
<code>ourDog.bark = "bow-wow";</code>
|
2018-09-30 23:01:58 +01:00
|
|
|
or
|
|
|
|
<code>ourDog["bark"] = "bow-wow";</code>
|
|
|
|
Now when we evaluate <code>ourDog.bark</code>, we'll get his bark, "bow-wow".
|
2020-03-25 08:07:13 -07:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```js
|
|
|
|
var ourDog = {
|
|
|
|
"name": "Camper",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["everything!"]
|
|
|
|
};
|
|
|
|
|
|
|
|
ourDog.bark = "bow-wow";
|
|
|
|
```
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
|
|
|
Add a <code>"bark"</code> property to <code>myDog</code> and set it to a dog sound, such as "woof". You may use either dot or bracket notation.
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
2018-10-04 14:37:37 +01:00
|
|
|
tests:
|
2019-11-27 02:57:38 -08:00
|
|
|
- text: You should add the property <code>"bark"</code> to <code>myDog</code>.
|
2019-07-13 00:07:53 -07:00
|
|
|
testString: assert(myDog.bark !== undefined);
|
2019-11-27 02:57:38 -08:00
|
|
|
- text: You should not add <code>"bark"</code> to the setup section.
|
2019-07-13 00:07:53 -07:00
|
|
|
testString: assert(!/bark[^\n]:/.test(code));
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Setup
|
|
|
|
var myDog = {
|
|
|
|
"name": "Happy Coder",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["freeCodeCamp Campers"]
|
|
|
|
};
|
|
|
|
|
2020-03-02 23:18:30 -08:00
|
|
|
// Only change code below this line
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2018-10-20 21:02:47 +03:00
|
|
|
(function(z){return z;})(myDog);
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
var myDog = {
|
|
|
|
"name": "Happy Coder",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["freeCodeCamp Campers"]
|
|
|
|
};
|
|
|
|
myDog.bark = "Woof Woof";
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|