2018-10-08 13:34:43 -04:00

2.3 KiB

id, title, localeTitle, challengeType
id title localeTitle challengeType
567af2437cbaa8c51670a16c Testing Objects for Properties Prueba de objetos para propiedades 1

Description

A veces es útil verificar si la propiedad de un objeto dado existe o no. Podemos usar el método de objetos .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

Modifique la función 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(&quot;gift&quot;)</code> debe devolver <code>&quot;pony&quot;</code> .
    testString: 'assert(checkObj("gift") === "pony", "<code>checkObj("gift")</code> should return  <code>"pony"</code>.");'
  - text: <code>checkObj(&quot;pet&quot;)</code> debe devolver <code>&quot;kitten&quot;</code> .
    testString: 'assert(checkObj("pet") === "kitten", "<code>checkObj("pet")</code> should return  <code>"kitten"</code>.");'
  - text: <code>checkObj(&quot;house&quot;)</code> debe devolver <code>&quot;Not Found&quot;</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";
  }
}