Testing Objects for Properties having trouble with writing the function

Tell us what’s happening:
How should i set it if the property is found. To true?
I can’t simpley seems to use if property XX === true
Your code so far


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

function checkObj(checkProp) {
// Your Code Here
if myObj.hasOwnProperty("gift"); //pony
else myObj.hasOwnProperty("pet"); // kitten
return "Not Found";
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.2 Safari/605.1.15.

Challenge: Testing Objects for Properties

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties

you can use dot notation so if hasOwnProperty ? return obj.key hope this makes sense

1 Like

From the challenge:

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

A couple of issues:

  1. Your syntax for the if statement is not correct. You need to put () around your conditional statement.
  2. Else statements should not have conditional statements.
  3. You shouldn’t be checking for "gift" or "pet". You should be checking for checkProp.
  4. You should return the value of the property if it is found.
1 Like

So like this?

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

function checkObj(checkProp) {
  // Your Code Here
if myObj.hasOwnProperty("myObj");
  return = "myObj";
  else if myObj.hasOwnProperty("");
  return = "Not Found";
}

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

the if statement has this syntax, and you can’t remove the parenthesis:

if (...) {
  ...
} else if (...) {
  ...
} else {
  ...
}

you can’t use the assignment operator with the return statement. if you want to return the variable myVar you write return myVar , if you want to return "Hi" you write return "Hi" and so on

1 Like

this still not right, your function has an argument that you aint using you should put that in the hasOwnProperty and all return = "myObj" is doing is returning a string of myObj so you need to fix that and you can brackets for that so obj[checkProp]

1 Like

Remember the prompt:

If the property is found, return that property’s value. If not, return "Not Found" .

I find it helpful to translate the sentence to code. To me, this looks like

if ( "the property is found" ) {
  "return that property’s value"
} else { // i.e. "If not"
  "return "Not Found""
}
1 Like

So like this?
but what do i need to put for found?
how do i check that function ?

Like if "checkProp"is there how do i set it to equal true?
my first thaught was if “checkProp == true” but that seems no good

function checkObj(checkProp) {
  // Your Code Here Modify the function checkObj
myObj.hasOwnProperty("checkProp");
if (found) { 
return "true"
} else (not found) {
  return "Not Found";
  }

found and not found don’t actually do anything, you need to put code there that will evaluate to true or false

1 Like

This exercise is about hasOwnProperty, so that should come into play at some point. Right now you are just calling myObj.hasOwnProperty("checkProp") but you aren’t saving or using the output value from that line. Also, checkProp is a variable, that holds the string you are comparing against.