Testing Objects for Properties Solution 1

Tell us what’s happening:

//First of all declare the obj object in the  beginning of code
var obj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh",
  //city: "Seattle"
}
//Do not change the parameters in function and use them 
function checkObj(obj, checkProp) {
//Sending as  a parameter obj objects' elemants to to property in if statement via //checkProp 
  if(obj.hasOwnProperty(checkProp)){
//We're returning obj object and looking for elements inside of it via checkPro //parameter
    return obj[checkProp];
  }else{
//If the element is not include in object as a property it should return Not Found string
    return "Not Found";
  }
}

//Let's testing your code here with gift
console.log(checkObj("gift"));

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36

Challenge: Testing Objects for Properties

Link to the challenge:

Your code looks incorrect but do you have a question?

remember that checkObj takes two parameters, not one

you absolutely don’t need to do this

I do not have a question but I would like to share my solution with it’s logic. And it was worked correctly.

My code was worked correctly with this format. If you want you can try to do it. I shared it to help people find a solution in this way. Also I will try your recommandation to apply thank for it

It works exactly the same if you delete the object. The function assigns whatever object is passed into it to the parameter obj in the function. The object you’ve declared does absolutely nothing, it’s not used anywhere in the code. Just because you’ve given the variable it’s assigned to the same name as one of the parameters doesn’t mean that the function uses it.

This piece of code:

var obj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh",
  city: "Seattle"
}

function checkObj(obj, checkProp) {
  if (obj.hasOwnProperty(checkProp)) {
    return obj[checkProp];
  } else {
    return "Not Found";
  }
}

Does exactly the same as this piece of code:

function checkObj(obj, checkProp) {
  if (obj.hasOwnProperty(checkProp)) {
    return obj[checkProp];
  } else {
    return "Not Found";
  }
}

There is just an unused object assigned to a variable obj in the first one.

1 Like

this one is the wrong way to call the function, see what it does:

1 Like

Yours is very similar to the old solutions here

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

function checkObj(checkProp) {
// Your Code Here
  if (myObj.hasOwnProperty(checkProp)) {
    return myObj[checkProp];
  } else {
    return “Not Found”;
  }

}

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

Looking up old solutions really isn’t helpful for two reasons

  1. The challenges receive updates frame time to time, so replicating old solutions will lead to confusing or wrong code. In this case, the function signature changed, and your current code half using the old function signature results in you writing buggy code.

  2. The point of the challenges isn’t to get the check marks. The point is to practice solving coding problems. Reading and modifying the code of others is an important skill, but it doesn’t help you learn how to write your own new code to solve coding problems.

If you get stuck, instead of looking up old solutions to try to replicate, I instead recommend asking questions here on the forum. We can help you get un stuck, but we won’t write out the solutions for you, so you still get to practice the key skills in solving coding problems while getting help.

3 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.