2.3 KiB
2.3 KiB
id, title, localeTitle, challengeType
id | title | localeTitle | challengeType |
---|---|---|---|
567af2437cbaa8c51670a16c | Testing Objects for Properties | Prueba de objetos para propiedades | 1 |
Description
.hasOwnProperty(propname)
para determinar si ese objeto tiene el nombre de propiedad dado. .hasOwnProperty()
devuelve true
o false
si se encuentra la propiedad o no.
Ejemplo
var myObj = {
top: "hat",
bottom: "pants"
};
myObj.hasOwnProperty("top"); // true
myObj.hasOwnProperty("middle"); // false
Instructions
checkObj
para probar myObj
para checkProp
. Si se encuentra la propiedad, devuelva el valor de esa propiedad. Si no, devuelve "Not Found"
.
Tests
tests:
- text: <code>checkObj("gift")</code> debe devolver <code>"pony"</code> .
testString: 'assert(checkObj("gift") === "pony", "<code>checkObj("gift")</code> should return <code>"pony"</code>.");'
- text: <code>checkObj("pet")</code> debe devolver <code>"kitten"</code> .
testString: 'assert(checkObj("pet") === "kitten", "<code>checkObj("pet")</code> should return <code>"kitten"</code>.");'
- text: <code>checkObj("house")</code> debe devolver <code>"Not Found"</code> .
testString: 'assert(checkObj("house") === "Not Found", "<code>checkObj("house")</code> should return <code>"Not Found"</code>.");'
Challenge Seed
// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
// Your Code Here
return "Change Me!";
}
// Test your code by modifying these values
checkObj("gift");
Solution
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
if(myObj.hasOwnProperty(checkProp)) {
return myObj[checkProp];
} else {
return "Not Found";
}
}