Testing Objects for Properties, basic JS

Because you are trying to get an object inside the variable. You have to put it as a string…

console.log(checkObj("gift"));

If you don’t put it as strings, JS will think that its a variable, and there is no variable inside the variable. You have to decalre it as a string, because an object inside a variable is a string.

I’m afraid that’s still doesn’t work. I’m not understanding what I am doing wrong. I changed it to a string when I called the function console.log(checkObj("gift")); just as you stated, but it still doesn’t render to the console. Funny how the code works, but it doesn’t render to the console. I even tried to “fix” redo/troubleshoot the declaration of the variable, but it still doesn’t work. If you could just point to me exactly what I need to do to print/render to the console I should be good after this. Thanks so much.

can you give me the link to the challenge?

Here you go: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties

So in the challenge, you have to give the objects one by one and then check it out to find the object ex;

console.log(checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift"));

You basically have to assign it first, and then find the object

1 Like

I am totally not understanding this. I am floored by this. If I call the function and pass it a parameter then it should return what I am asking for. Every other challenge worked this way. Even the challenge just before this worked this way. I call the function, pass a parameter and it renders the “property” or the “value” of the object. I declared the Object as a variable with all of the properties and values right after the function. Once I called the function it should render… I must be missing something… sorry… but something isn’t clicking for me. The only basic difference between this Challenge and the previous Challenge is that I am asking the Object to return a value if I pass in a property as opposed to this Challenge where I am asking for it to return a property.

Previous Challenge link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups

Its fine, It was really hard for me to understand at first to. I still don’t get it why it does that either.

The function has two parameters (an object and a property), it isn’t going to work if you don’t pass both of them into the function.

This one works exactly the same way: if on any of the other challenges you just didn’t pass the parameter, the function would not return the value you expected.

What you have done is hardcoded an object into the body of the function. The challenge doesn’t ask for this. I think this is what’s confusing you, you’ve added an extra thing to the challenge.

function checkObj(obj, checkProp) {

The function has two parameters: an object and a property name (just a string). Then the function checks whether that property name exists on the object.

1 Like

So what you’re telling me is that I can delete the “var = myObj …” part of the code and I should be able to get it to render in the console? If I don’t declare the Object somewhere within the code how does it know what object that I’m looking for? Thanks in advance.

Yes. Look at the function:

function checkObj(obj, checkProp)

It only “knows” what the object is because that’s the thing that is passed to the function as the first argument. For another example:

function add(x, y) {
  return x + y;
}

It “knows” what x and y are if and only if you give the function the two numbers it expects: you have to call it like add(1, 1), add(1) is not going to work

1 Like

I understand the code when explaining it in mathematical terms, but when talking about an Object property and the value of a property (key:value pairs) of the Object it is not comparable. The code only works when I declare the Object as a variable with all of the key:value pairs - property:value of the Object, but it still doesn’t render in the console when I call the function and pass in the parameter. Here is the code with the Object being declared as a variable deleted:

function checkObj(obj, checkProp) {
  // Only change code below this line
    
  	if (obj.hasOwnProperty(checkProp)) {
      return obj[checkProp];  
  } 	else {
     	return "Not Found";
  }
   
  // Only change code above this line
} console.log(checkObj());

The above code doesn’t work. Maybe you can tell me what’s wrong with it. Compared to the first code I wrote/submitted above this doesn’t compare. It just doesn’t work. I’ve spent a considerable amount of time on this. I’ve watched the video for help several times. This is same code that is in the video. But it doesn’t work. Thank you in advance. What I did notice/learn is that in the previous lesson the “Object” was declared as a variable and when you called the function and passed in a parameter “the property” it returned the “value” of that specific “property” of the Object. It was real clear cut… simple. The only difference with this Challenge is the .hasOwnProperty method. My understanding is we want to pass in a parameter to the function - a “value” - and the function will search the Object to see if a “property” exists for that specific "value ". I believe the “naming” of the differing Objectives, Properties, and Values in the code is what confuses one. What I’m not understanding is this: How can you have an Object if you don’t declare it first? - with all of the properties and values -key:value pairs inside of the Object? Thank you again.

  • checkObj is a function
  • the function takes two parameters, not one. Functions can have as many parameters as you want, but you can’t just then ignore them and expect it to work.
  • the first parameter is an object.
  • therefore you need to give the function an object as the first argument.

function exampleFunction(input) {
  return input;
}

This is function that takes some input and returns it. If you do not give it the input it will not produce the output.

So if I give it an object, it returns an object.

example({ foo: 1})

That returns {foo:1}. The object hasn’t appeared from thin air, I literally put it into the function.


You can’t.

Of course it doesn’t work. JavaScript isn’t magic: it doesn’t “know” what you want it to do. You have a function that needs two things given to it to work. You are giving it none. It won’t work.

To go back to the example I used in the last post:

function add(x, y) {
  return x + y;
}

This is a function that adds two numbers. If you give it two numbers, it adds them up. If you try to call it without two numbers, it can’t add them together because they won’t exist – calling add() can’t possibly do anything

You are almost there, you just seem to be hugely overcomplicating this in your head.

  • your are being asked to write a function with two parameters.
  • one of those is an object (parameter 1, obj)
  • one of those is the name of a property that might be on that object (parameter 2, checkProp)
  • inside the function you use a function JS makes available to you (hasOwnProperty) to see if parameter1 (obj) has parameter2 (checkProp)
1 Like

What can I do when I get completely stuck? If you could show me exactly what I need to correct my code I could move on. It’s apparent it’s something very minor that I’m not connecting with in the code in this challenge. All of the other challenges I have completed with comparative ease - rather smoothly. The system accepts my code. The only thing I want is for it to be able to perform a “console.log” so it can render/print an answer in the console. I’ve written the code exactly as it is displayed in the “help video” for this challenge. I’ve passed the arguments to the function as written to no avail. What is the last resort? Thank you in advance. Based on the code that I stated was working above, what specifically do I need to correct in this code?

function checkObj(obj, checkProp) {
 // Only change code below this line
   var result = "";
   var myObj  = {
     gift:"pony",
     pet: "kitten",
     bed: "sleigh",
     city:"Seattle"
 }
 	if (myObj.hasOwnProperty(checkProp)) {
     result = obj[checkProp];
     return result;  
 } 	else {
    	return "Not Found";
 }
  
 // Only change code above this line
} console.log(checkObj("gift"));

How can I fix this? What do I need to take out or add to fix this? It has to be something simple. PS I’m passing all of the challenges after this one so it is not that I’m not gathering an understanding of coding or what’s going on.

Correct me if I’m mistaken, but I believe that " key : value " pairs make up the property, and the “property” or “properties” make up the object. So this challenge is asking us to to use a “key” to access - “render” - the “value” of the property. Correct me if I’m wrong?

You do not need to manually hardcode an object into the function – you can’t possibly know what it is, because it gets passed in as the first argument. To go back to one of my examples:

function add(a, b) {
  return 100 + b;
}

This is clearly wrong. I would expect that add(2, 2) would return 4, but it doesn’t, it returns 102. You have done the exact same thing – instead of using the first argument, you’ve ignored it and defined your own object.

[Note that following contains the actual answer]

The function checkObj takes an object (obj) and a string (the name of a property to check, hence checkProp).

if that obj has the property (hasOwnProperty) checkProp, then return the property (the value of the property with the key checkProp, so accessed obj[checkProp]). Otherwise return the string "Not Found"

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

console.log(checkObj({foo: 1}, "foo")); // logs 1
console.log(checkObj({foo: 1}, "bar")); // logs "Not Found"
console.log(checkObj({foo: 1, bar: 2}, "bar")); // logs 2
1 Like