* fix: remove isHidden flag from frontmatter * fix: add isUpcomingChange Co-authored-by: Ahmad Abdolsaheb <ahmad.abdolsaheb@gmail.com> * feat: hide blocks not challenges Co-authored-by: Ahmad Abdolsaheb <ahmad.abdolsaheb@gmail.com> Co-authored-by: Ahmad Abdolsaheb <ahmad.abdolsaheb@gmail.com>
2.9 KiB
2.9 KiB
id, title, challengeType, videoUrl, forumTopicId
id | title | challengeType | videoUrl | forumTopicId |
---|---|---|---|---|
56533eb9ac21ba0edf2244c9 | Accessing Object Properties with Variables | 1 | https://scrimba.com/c/cnQyKur | 16165 |
Description
var dogs = {
Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle"
};
var myDog = "Hunter";
var myBreed = dogs[myDog];
console.log(myBreed); // "Doberman"
Another way you can use this concept is when the property's name is collected dynamically during the program execution, as follows:
var someObj = {
propName: "John"
};
function propPrefix(str) {
var s = "prop";
return s + str;
}
var someProp = propPrefix("Name"); // someProp now holds the value 'propName'
console.log(someObj[someProp]); // "John"
Note that we do not use quotes around the variable name when using it to access the property because we are using the value of the variable, not the name.
Instructions
playerNumber
variable to 16
. Then, use the variable to look up the player's name and assign it to player
.
Tests
tests:
- text: <code>playerNumber</code> should be a number
testString: assert(typeof playerNumber === 'number');
- text: The variable <code>player</code> should be a string
testString: assert(typeof player === 'string');
- text: The value of <code>player</code> should be "Montana"
testString: assert(player === 'Montana');
- text: You should use bracket notation to access <code>testObj</code>
testString: assert(/testObj\s*?\[.*?\]/.test(code));
- text: You should not assign the value <code>Montana</code> to the variable <code>player</code> directly.
testString: assert(!code.match(/player\s*=\s*"|\'\s*Montana\s*"|\'\s*;/gi));
- text: You should be using the variable <code>playerNumber</code> in your bracket notation
testString: assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code));
Challenge Seed
// Setup
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
// Only change code below this line
var playerNumber; // Change this line
var player = testObj; // Change this line
After Test
if(typeof player !== "undefined"){(function(v){return v;})(player);}
Solution
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
var playerNumber = 16;
var player = testObj[playerNumber];