Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

98 lines
2.5 KiB
Markdown

---
id: 567af2437cbaa8c51670a16c
title: Testing Objects for Properties
challengeType: 1
forumTopicId: 18324
dashedName: 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)` returns `true` or `false` depending on if the property is found on the object or not.
**Example**
```js
function checkForProperty(object, property) {
return object.hasOwnProperty(property);
}
checkForProperty({ top: 'hat', bottom: 'pants' }, 'top'); // true
checkForProperty({ top: 'hat', bottom: 'pants' }, 'middle'); // false
```
The first `checkForProperty` function call returns `true`, while the second returns `false`.
# --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. If not, return `Not Found`.
# --hints--
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")` should return the string `pony`.
```js
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'gift') === 'pony'
);
```
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")` should return the string `kitten`.
```js
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'pet') === 'kitten'
);
```
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")` should return the string `Not Found`.
```js
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'house') ===
'Not Found'
);
```
`checkObj({city: "Seattle"}, "city")` should return the string `Seattle`.
```js
assert(checkObj({ city: 'Seattle' }, 'city') === 'Seattle');
```
`checkObj({city: "Seattle"}, "district")` should return the string `Not Found`.
```js
assert(checkObj({ city: 'Seattle' }, 'district') === 'Not Found');
```
`checkObj({pet: "kitten", bed: "sleigh"}, "gift")` should return the string `Not Found`.
```js
assert(checkObj({ pet: 'kitten', bed: 'sleigh' }, 'gift') === 'Not Found');
```
# --seed--
## --seed-contents--
```js
function checkObj(obj, checkProp) {
// Only change code below this line
return "Change Me!";
// Only change code above this line
}
```
# --solutions--
```js
function checkObj(obj, checkProp) {
if(obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return "Not Found";
}
}
```