3.9 KiB
3.9 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
56bbb991ad1ed5201cd392d0 | Build JavaScript Objects | 1 |
Description
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
.
Objects are useful for storing data in a structured way, and can represent real world objects, like a cat.
Here's a sample cat object:
var cat = {In this example, all the properties are stored as strings, such as -
"name": "Whiskers",
"legs": 4,
"tails": 1,
"enemies": ["Water", "Dogs"]
};
"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:
var anotherObject = {However, if your object has any non-string properties, JavaScript will automatically typecast them as strings.
make: "Ford",
5: "five",
"model": "focus"
};
Instructions
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 "name"
is a string, "legs"
and "tails"
are numbers, and "friends"
is an array.
Tests
- text: <code>myDog</code> should contain the property <code>name</code> and it should be a <code>string</code>.
testString: 'assert((function(z){if(z.hasOwnProperty("name") && z.name !== undefined && typeof z.name === "string"){return true;}else{return false;}})(myDog), ''<code>myDog</code> should contain the property <code>name</code> and it should be a <code>string</code>.'');'
- text: <code>myDog</code> should contain the property <code>legs</code> and it should be a <code>number</code>.
testString: 'assert((function(z){if(z.hasOwnProperty("legs") && z.legs !== undefined && typeof z.legs === "number"){return true;}else{return false;}})(myDog), ''<code>myDog</code> should contain the property <code>legs</code> and it should be a <code>number</code>.'');'
- text: <code>myDog</code> should contain the property <code>tails</code> and it should be a <code>number</code>.
testString: 'assert((function(z){if(z.hasOwnProperty("tails") && z.tails !== undefined && typeof z.tails === "number"){return true;}else{return false;}})(myDog), ''<code>myDog</code> should contain the property <code>tails</code> and it should be a <code>number</code>.'');'
- text: <code>myDog</code> should contain the property <code>friends</code> and it should be an <code>array</code>.
testString: 'assert((function(z){if(z.hasOwnProperty("friends") && z.friends !== undefined && Array.isArray(z.friends)){return true;}else{return false;}})(myDog), ''<code>myDog</code> should contain the property <code>friends</code> and it should be an <code>array</code>.'');'
- text: <code>myDog</code> should only contain all the given properties.
testString: 'assert((function(z){return Object.keys(z).length === 4;})(myDog), ''<code>myDog</code> should only contain all the given properties.'');'
Challenge Seed
// Example
var ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"]
};
// Only change code below this line.
var myDog = {
};
After Test
console.info('after the test');
Solution
var myDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"]
};