2018-09-30 23:01:58 +01:00
---
id: 56bbb991ad1ed5201cd392d0
title: Build JavaScript Objects
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cWGkbtd'
2019-07-31 11:32:23 -07:00
forumTopicId: 16769
2021-01-13 03:31:00 +01:00
dashedName: build-javascript-objects
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
You may have heard the term `object` before.
Objects are similar to `arrays` , except that instead of using indexes to access and modify their data, you access the data in objects through what are called `properties` .
2018-09-30 23:01:58 +01:00
Objects are useful for storing data in a structured way, and can represent real world objects, like a cat.
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
Here's a sample cat object:
2019-05-17 06:20:30 -07:00
```js
var cat = {
"name": "Whiskers",
"legs": 4,
"tails": 1,
"enemies": ["Water", "Dogs"]
};
```
2020-11-27 19:02:05 +01:00
In this example, all the properties are stored as strings, such as - `"name"` , `"legs"` , and `"tails"` . However, you can also use numbers as properties. You can even omit the quotes for single-word string properties, as follows:
2019-05-17 06:20:30 -07:00
```js
var anotherObject = {
make: "Ford",
5: "five",
"model": "focus"
};
```
2018-09-30 23:01:58 +01:00
However, if your object has any non-string properties, JavaScript will automatically typecast them as strings.
2020-11-27 19:02:05 +01:00
# --instructions--
Make an object that represents a dog called `myDog` which contains the properties `"name"` (a string), `"legs"` , `"tails"` and `"friends"` .
You can set these object properties to whatever values you want, as long as `"name"` is a string, `"legs"` and `"tails"` are numbers, and `"friends"` is an array.
# --hints--
`myDog` should contain the property `name` and it should be a `string` .
```js
assert(
(function (z) {
if (
z.hasOwnProperty('name') & &
z.name !== undefined & &
typeof z.name === 'string'
) {
return true;
} else {
return false;
}
})(myDog)
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`myDog` should contain the property `legs` and it should be a `number` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
(function (z) {
if (
z.hasOwnProperty('legs') & &
z.legs !== undefined & &
typeof z.legs === 'number'
) {
return true;
} else {
return false;
}
})(myDog)
);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`myDog` should contain the property `tails` and it should be a `number` .
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(
(function (z) {
if (
z.hasOwnProperty('tails') & &
z.tails !== undefined & &
typeof z.tails === 'number'
) {
return true;
} else {
return false;
}
})(myDog)
);
```
2018-10-08 01:01:53 +01:00
2020-11-27 19:02:05 +01:00
`myDog` should contain the property `friends` and it should be an `array` .
2018-10-08 01:01:53 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
(function (z) {
if (
z.hasOwnProperty('friends') & &
z.friends !== undefined & &
Array.isArray(z.friends)
) {
return true;
} else {
return false;
}
})(myDog)
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`myDog` should only contain all the given properties.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
(function (z) {
return Object.keys(z).length === 4;
})(myDog)
);
```
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
(function(z){return z;})(myDog);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
```js
var myDog = {
// Only change code below this line
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
// Only change code above this line
};
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
var myDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
2018-10-08 01:01:53 +01:00
"friends": ["everything!"]
2018-09-30 23:01:58 +01:00
};
```