2.3 KiB
2.3 KiB
id, title, challengeType, videoUrl, forumTopicId
id | title | challengeType | videoUrl | forumTopicId |
---|---|---|---|---|
56533eb9ac21ba0edf2244c7 | Accessing Object Properties with Dot Notation | 1 | https://scrimba.com/c/cGryJs8 | 16164 |
Description
.
) and bracket notation ([]
), similar to an array.
Dot notation is what you use when you know the name of the property you're trying to access ahead of time.
Here is a sample of using dot notation (.
) to read an object's property:
var myObj = {
prop1: "val1",
prop2: "val2"
};
var prop1val = myObj.prop1; // val1
var prop2val = myObj.prop2; // val2
Instructions
testObj
using dot notation. Set the variable hatValue
equal to the object's property hat
and set the variable shirtValue
equal to the object's property shirt
.
Tests
tests:
- text: <code>hatValue</code> should be a string
testString: assert(typeof hatValue === 'string' );
- text: The value of <code>hatValue</code> should be <code>"ballcap"</code>
testString: assert(hatValue === 'ballcap' );
- text: <code>shirtValue</code> should be a string
testString: assert(typeof shirtValue === 'string' );
- text: The value of <code>shirtValue</code> should be <code>"jersey"</code>
testString: assert(shirtValue === 'jersey' );
- text: You should use dot notation twice
testString: assert(code.match(/testObj\.\w+/g).length > 1);
Challenge Seed
// Setup
var testObj = {
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
// Only change code below this line
var hatValue = testObj; // Change this line
var shirtValue = testObj; // Change this line
After Test
(function(a,b) { return "hatValue = '" + a + "', shirtValue = '" + b + "'"; })(hatValue,shirtValue);
Solution
var testObj = {
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
var hatValue = testObj.hat;
var shirtValue = testObj.shirt;