* fix: Chinese test suite Add localeTiltes, descriptions, and adjust test text and testStrings to get the automated test suite working. * fix: ran script, updated testStrings and solutions
2.7 KiB
2.7 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
56533eb9ac21ba0edf2244c9 | Accessing Object Properties with Variables | 1 | 使用变量访问对象属性 |
Description
var dogs = {另一种可以使用此概念的方法是在程序执行期间动态收集属性的名称,如下所示:
Fido:“Mutt”,Hunter:“Doberman”,Snoopie:“Beagle”
};
var myDog =“猎人”;
var myBreed = dogs [myDog];
的console.log(myBreed); //“杜宾犬”
var someObj = {请注意,在使用变量名来访问属性时,我们不会使用引号,因为我们使用的是变量的值 ,而不是名称 。
propName:“约翰”
};
function propPrefix(str){
var s =“prop”;
return s + str;
}
var someProp = propPrefix(“Name”); // someProp现在保存值'propName'
的console.log(someObj中[someProp]); // “约翰”
Instructions
playerNumber
变量使用括号表示法在testObj
查找玩家16
。然后将该名称分配给player
变量。 Tests
tests:
- text: <code>playerNumber</code>应该是一个数字
testString: assert(typeof playerNumber === 'number');
- text: 变量<code>player</code>应该是一个字符串
testString: assert(typeof player === 'string');
- text: <code>player</code>的价值应该是“蒙大拿”
testString: assert(player === 'Montana');
- text: 您应该使用括号表示法来访问<code>testObj</code>
testString: assert(/testObj\s*?\[.*?\]/.test(code));
- text: 您不应该直接将值<code>Montana</code>分配给变量<code>player</code> 。
testString: assert(!code.match(/player\s*=\s*"|\'\s*Montana\s*"|\'\s*;/gi));
- text: 您应该在括号表示法中使用变量<code>playerNumber</code>
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
console.info('after the test');
Solution
// solution required