mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-30 12:05:39 -05:00
2.4 KiB
2.4 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 567af2437cbaa8c51670a16c | 測試對象的屬性 | 1 | 18324 | testing-objects-for-properties |
--description--
要檢查某個對象是否具有一個屬性,你可以使用 .hasOwnProperty() 方法。 根據對象是否具有該屬性,someObject.hasOwnProperty(someProperty) 返回 true 或 false。
示例
function checkForProperty(object, property) {
return object.hasOwnProperty(property);
}
checkForProperty({ top: 'hat', bottom: 'pants' }, 'top'); // true
checkForProperty({ top: 'hat', bottom: 'pants' }, 'middle'); // false
第一個 checkForProperty 函數返回 true,第二個返回 false。
--instructions--
修改函數 checkObj 以檢查傳給函數參數的對象 obj 是否包含傳給函數參數的屬性 checkProp。 如果在 obj 中找到傳給 checkProp 的屬性,則返回該屬性的值。 如果沒有,則返回 Not Found。
--hints--
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") 應該返回字符串 pony。
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'gift') === 'pony'
);
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet") 應該返回字符串 kitten。
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'pet') === 'kitten'
);
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house") 應該返回字符串 Not Found。
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'house') ===
'Not Found'
);
checkObj({city: "Seattle"}, "city") 應該返回字符串 Seattle。
assert(checkObj({ city: 'Seattle' }, 'city') === 'Seattle');
checkObj({city: "Seattle"}, "district") 應該返回字符串 Not Found。
assert(checkObj({ city: 'Seattle' }, 'district') === 'Not Found');
checkObj({pet: "kitten", bed: "sleigh"}, "gift") 應該返回字符串 Not Found。
assert(checkObj({ pet: 'kitten', bed: 'sleigh' }, 'gift') === 'Not Found');
--seed--
--seed-contents--
function checkObj(obj, checkProp) {
// Only change code below this line
return "Change Me!";
// Only change code above this line
}
--solutions--
function checkObj(obj, checkProp) {
if(obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return "Not Found";
}
}