Pls explain and find where I made a mistake

Tell us what’s happening:

I do not understand what the
And even the syntax

Your code so far


function checkObj(obj, checkProp) {
// Only change code below this line
if(checkObj.hasOwnProperty(checkProp)){
return obj[checkProp];

}else{
  return "Not Found";
}
//return "Change Me!";
// Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 8.1.0; Infinix X5515 Build/O11019) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Mobile Safari/537.36.

Challenge: Testing Objects for Properties

Link to the challenge:

your mistake is in the if
if(checkObj.hasOwnProperty(checkProp))
There you are calling the function instead of the object that was passed as a parameter to the function

Pls explain in details
Sir

Aight, so
This function works to see whether an object has a property. If it does have the property. It will return the value held in the property. If it does not have the property, it will return “Not Found”

It takes in obj and checkProp as properties. This is so that you can use the function on any object elsewhere since you’ll pass in the name of the object you want to check as the first parameter and the name of the property that you want to confirm is in the object as the second parameter.

Where you went wrong is in the if statement. The hasOwnProperty method won’t work on a function. It only works on an object. And in this case you have an object which you passed as a parameter to the function

If you remember in object lookups where you go obj[propertyName]
This is the same case here
It’s checking if obj has a property called propertyName and if it does. Return obj[propertyName]

Except in this case checkProp is the propertyName

Pls just make an example with function and explain the way it works
Thanks for help

let’s say i had an object:

 const Numbers = {
    one : 1,
    two : 2,
    three : 3
}

and i wanted to see whether there is a number 3 in the object. I would use checkObj as follows
checkObj(Numbers, three)
This would return 3 since Numbers does indeed have a property called three
if i did checkObj(Numbers, four) it would return Not Found since there is no property called four in Numbers

1 Like

Hi!

You are on the right way with your code but there is a typo in it!
You have to check if the parameter of the function obj has hasOwnProperty(checkProp) and not the function itself!
@JudgeFudge19 has already mentioned it!
If you have more questions do not hesitate to contact us!

Why doesn’t this work

var myObj = {
   gift:"pony",
  pet:"kitten"
  bed:"sleigh"
   }
function checkObj(checkProp){
     if(myObj.hasOwnProperty(checkProp){
    return myObj.checkProp;
}else{
   return "Not Found" } ```
Pls have look becoming angry😅
@rielka pls have a look too

Hi,

It does not work because it is an other function as excepted in the challenge!
The function is:
function checkObj(obj, checkProp)
You have an Object ‘ob’j’ and you should check if it has ‘hasOwnProperty’!

Your first code was really OK!
Instead of using:
if(checkObj.hasOwnProperty(checkProp))
replace it with:
if(obj.hasOwnProperty(checkProp))
and do not change the rest of the code and your code will work!
And above all forget the code that you have just posted!

You change and added code you were not asked to. Reset the challenge and only add code where the comments tell you to.

Your initial code is 95% correct. All you have to do is use hasOwnProperty on obj and not on the function checkObj.

The parameter obj is the object, the parameter checkProp is the property.

Like this

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

Cos it doesn’t work for me
And when I try to understand the syntax
function checkObj(obj(*why didn't the give us a real obj to work on*,checkProp)
If I am to console.log what’s above how will i write it
Pls @arielka help😔

You are missing the closing ) for the if statment condition.

if( obj.hasOwnProperty(checkProp) )

Tell us what’s happening:
I can not spot the error here
Pls help

Your code so far


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

function checkObj(checkProp) {
 if(myObj.hasOwnProperty(checkProp)){
   return myObj[checkProp];
 }else{
   return "Not Found"
 }
 // Only change code below this line
// return "Change Me!";
 // Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 8.1.0; Infinix X5515 Build/O11019) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Mobile Safari/537.36.

Challenge: Testing Objects for Properties

Link to the challenge:

You have added a bunch of code that you shouldn’t.

This is the starter code:

function checkObj(obj, checkProp) {
  // Only change code below this line
  return "Change Me!";
  // Only change code above this line
}

You added code outside of the Only change code comments, which is causing your problems.

Specifically, you changed the list of arguments for the function and modified the function to only work for a single specific global object.

You perhaps have copied and pasted a very old solution to a previous version of this problem.

Pls explain
Cos I don’t know how to work with this
function checkObj(obj,checkProp)

What part about that function signature is confusing you?

The first argument is the object that you need to check and checkProp is the property you are looking for.

If I am to console.log the function it how will I write it

@udohrichard16novembe Please do not create duplicate threads for the same challenge.

I have merged the two threads.