2.5 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 567af2437cbaa8c51670a16c | Objekte auf Eigenschaften prüfen | 1 | 18324 | testing-objects-for-properties |
--description--
To check if a property on a given object exists or not, you can use the .hasOwnProperty() method. someObject.hasOwnProperty(someProperty) gibt true oder false zurück, je nachdem, ob die Eigenschaft auf dem Objekt gefunden wird oder nicht.
Beispiel
function checkForProperty(object, property) {
return object.hasOwnProperty(property);
}
checkForProperty({ top: 'hat', bottom: 'pants' }, 'top'); // true
checkForProperty({ top: 'hat', bottom: 'pants' }, 'middle'); // false
Der erste checkForProperty-Funktionsaufruf gibt true zurück, während der zweite false zurückgibt.
--instructions--
Modify the function checkObj to test if the object passed to the function parameter obj contains the specific property passed to the function parameter checkProp. If the property passed to checkProp is found on obj, return that property's value. Wenn nicht, wird Not Found zurückgegeben.
--hints--
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") sollte den String pony zurückgeben.
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'gift') === 'pony'
);
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet") sollte den String kitten zurückgeben.
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'pet') === 'kitten'
);
checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house") sollte den String Not Found zurückgeben.
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'house') ===
'Not Found'
);
checkObj({city: "Seattle"}, "city") sollte den String Seattle zurückgeben.
assert(checkObj({ city: 'Seattle' }, 'city') === 'Seattle');
checkObj({city: "Seattle"}, "district") sollte den String Not Found zurückgeben.
assert(checkObj({ city: 'Seattle' }, 'district') === 'Not Found');
checkObj({pet: "kitten", bed: "sleigh"}, "gift") sollte den String Not Found zurückgeben.
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";
}
}