Testing Objects for Properties, basic JS

Tell us what’s happening:
This code is recommended by many in “Get Hint” section and official video help explanation,
but the code didn’t pass the test when I run it. Got stuck here.

Your code so far
function checkObj(obj, checkProp) {

// Only change code below this line

if(myObj.hasOwnProperty(checkProp)){

return myObj[checkProp];

}

else if(myObj.hasOwnProperty(checkProp) !== true){

return “Not Found”;

}

else{

return “Change Me!”;

}

}

// Only change code above this line

}


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

Your browser information:

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

Challenge: Testing Objects for Properties

Link to the challenge:

the function has two parameters, are you using both of them?

Yes I follow the instructions
Here is the code I’m trying to run but it doesn’t work.
function checkObj(obj, checkProp) {

// Only change code below this line

if (myObj.hasOwnProperty(checkProp)) {

return myObj[checkProp];

} else {

return “Not Found”;

}

}

// Only change code above this line

}

Having the same issue. Unable to pass it.

It’s asking for example:

checkObj({city: "Seattle"}, "city") should return "Seattle" .

Code:

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

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

// Test your code by modifying these values
console.log(checkObj(“city”));

  • When I type gift, it returns pony
  • When I type pet, it returns kitten
  • When I type bed, it returns sleigh
  • When I type city, it returns Seattle.

I have no clue why this isn’t passing.

and where are you using obj in your function?

wrong seed, new version of the challenger has two parameters in the function, you need to reset your code

What do you mean with seed? I have just reset my code but the same thing happens. checkProp is the only parameter?

Oh my gosh, I got it. Thanks @ilenia. Now passed.

code seed
the code that appear as starting point

glad you made it! happy coding!

1 Like

Thanks @ilenia! Appreciated your help. Happy coding too :slight_smile:

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

ME facing the same issue and in my case no object property been provided very hard to manipulate.

Hello. In the if condition and also inside the if block, the object should be obj not myObj. What ever object you pass through the function is stored in the obj parameter. Hence you need to use obj to access the object you passed through the function arguments. After correcting this, there is no need for you to use the else if condition here, just use else and return “Not found”.

@sumitsinha215 you can see example objects in the tests, they would all be passed in the function in the parameter obj
(you have the object to manipulate in the function parameter)

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

if (myObj.hasOwnProperty(checkProp)) {
return myObj[checkProp]
} else {
return “Not Found”;
}
}

1 Like

but mam it’s not accepting,because before the function no object detail given and if i create myself it won’t accepting.

yes, there is no object defined because the function should work with any object. the object to test is passed in as function argument, so to try you can have an object and a function call

function checkObj(obj, checkProp) {
   // check if obj has the property 
}

const testObj = {
   "mood": "happy",
   "age": "99"
}

checkObj(testObj, "mood"); // should return "happy"
checkObj(testObj, "style"); // should return "Not Found"
checkObj({"dessert": "tiramisù"}, "dessert"); // should return "tiramisù"

any object can be passed in as function argument and the function should work with any object, so you must use the function parameters to reference the passed in object and the string inside the function

ok mam ,i try but help me with few more codes.

Basic JavaScript: Replace Loops using Recursion

function sum(arr, n) {
// Only change code below this line
if (n >= 0) {
return arr[0];
} else {
return sum(arr, n - 1) + arr[n-1];
}
}
// Only change code above this line
sum([2,3,4],1);
sum([2,3,4,5],3);
sum([1],0);

two case not satisfying.
link:https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion

It’s not myObj, it is obj and get rid of the
else{
return “Change Me!”;
}

I just need a little help with this code on the same topic. I hope I am posting this in the right forum. It’s the same topic. Here’s my code:

function checkObj(obj, checkProp) {
  // Only change code below this line
  var myObj  = {
    gift:"pony",
    pet: "kitten",
    bed: "sleigh",
    city:"Seattle"
  }

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

can you give me some pointers on where I went wrong

It’s fine, but don’t be afraid to create a new topic. And make sure to give an explanation of what is wrong with your code Also, can you give the link to the challenge?

I figured it out. But I’m not understanding how come I can’t get it to print /render to the console? If I call the function wrapped in console.log as such:

console.log(checkObj(gift)); 

It should print to the console, but it doesn’t. I’ve figured out the code… Also thanks for the info on being able to create a new topic.