Files
Randell Dawson 1494a50123 fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles
* fix: added 3 dashes line before prob expl
* fix: added 3 dashes line before hints
* fix: added 3 dashes line before solutions
2019-07-24 13:29:27 +05:30

1.1 KiB
Raw Blame History

title
title
Testing Objects for Properties

Testing Objects for Properties


Solutions

Solution 1 (Click to Show/Hide)

We do not change anything here:

// Setup
var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

further, in the body of the function we use .hasOwnProperty(propname) method of objects to determine if that object has the given property name. if/else statement with Boolean Values will help us in this:

function checkObj(checkProp) {
  // Your Code Here
  if (myObj.hasOwnProperty(checkProp) == true) {
    return myObj[checkProp];
  }
  else {
 //  and change the value of `return` in `else` statement:
 
    return "Not Found"
  }
}

Now, you can change checkObj values:

// Test your code by modifying these values
checkObj("gift");

Heres a full solution:

function checkObj(checkProp) {
  // Your Code Here
  if (myObj.hasOwnProperty(checkProp) == true) {
    return myObj[checkProp];
  } else {
    return "Not Found";
  }
}
// Test your code by modifying these values
checkObj("gift");