Randell Dawson 05f73ca409 fix(curriculum): Convert blockquote elements to triple backtick syntax for JavaScript Algorithms and Data Structures (#35992)
* fix: convert js algorithms and data structures

* fix: revert some blocks back to blockquote

* fix: reverted comparison code block to blockquotes

* fix: change js to json

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: convert various section to triple backticks

* fix: Make the formatting consistent for comparisons
2019-05-17 08:20:30 -05:00

2.1 KiB

id, title, challengeType, videoUrl
id title challengeType videoUrl
567af2437cbaa8c51670a16c Testing Objects for Properties 1 https://scrimba.com/c/cm8Q7Ua

Description

Sometimes it is useful to check if the property of a given object exists or not. We can use the .hasOwnProperty(propname) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not. Example
var myObj = {
  top: "hat",
  bottom: "pants"
};
myObj.hasOwnProperty("top");    // true
myObj.hasOwnProperty("middle"); // false

Instructions

Modify the function checkObj to test myObj for checkProp. If the property is found, return that property's value. If not, return "Not Found".

Tests

tests:
  - text: <code>checkObj("gift")</code> should return  <code>"pony"</code>.
    testString: assert(checkObj("gift") === "pony", '<code>checkObj("gift")</code> should return  <code>"pony"</code>.');
  - text: <code>checkObj("pet")</code> should return  <code>"kitten"</code>.
    testString: assert(checkObj("pet") === "kitten", '<code>checkObj("pet")</code> should return  <code>"kitten"</code>.');
  - text: <code>checkObj("house")</code> should return  <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";
  }
}