Stuck on a challengein JavaScript

Tell us what’s happening:
Describe your issue in detail here.
Ive tried everyway i knew but I’m not able to solve every problem in it.

  **Your code so far**

function checkObj(obj, checkProp) {
// Only change code below this line
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/96.0.4664.45 Safari/537.36 Edg/96.0.1054.29

Challenge: Testing Objects for Properties

Link to the challenge:

Hi @souhadracool,
Welcome to the Forums!
What aspect of this is it that you are struggling with exactly?

Try starting with an if / else statement

the if tests if obj has the property checkProp and returns the desired answer
the else returns the opposite.

1 Like

Hi
It looks like you Reset All your Code. . . .

If you look at the test data (below ‘Get Help’), you can see that they are looking for your understanding of finding objects by using keys.
e.g.
checkObj({gift: “pony”, pet: “kitten”, bed: “sleigh”}, “gift”) should return the string pony.

The parameters passed to your function, ‘checkObj’, are ‘obj’ and ‘checkProp’.
From the example above, ‘obj’ would be the Object:
{gift: “pony”, pet: “kitten”, bed: “sleigh”}
and ‘checkProp’ would be:
“gift”

You can see that ‘gift’ is part of the object (obj), so you know to expect it to return ‘true’ from that set of test data.

You would put your if-statements in place and use console.log to see what is going on. If you want to see what is in checkProp, for instance, use:

console.log(checkProp);
console.log("chP", checkProp);
console.log("isFnd", isFound);
console.log("k / i", key,  i, "num", numTries);
console.log("i - j - total", i, j, total);

It will show the value of ‘checkProp’ at that point.
The text ‘chP’ will just make it easier to see which line of the console.log you are looking at.
You can also see what is in a test variable, like isFound (boolean), or which element is being processed in a for-loop (mostly I use ‘i’), or keep track of the number of times it has gone through a while loop (numTries), or a running total, etc.

When you ‘Run the Tests’, be sure to have the Terminal showing, so you can see what is going on. If you cannot see the Terminal in the bottom right corner of your screen, hover over the bottom line / edge of the border at the bottom right and drag it up. Then more of the Terminal will show. Drag it down when you are typing in code and drag it up when you are running tests.

Hope it helps.

1 Like

Hi , @damianicely
I tried it with if else too,
I know I’m making a mistake

  if (obj){
    obj.hasOwnProperty(checkProp)
  }else{

  return "Not Found";

thats my code
I’m so washed I just cant figure it out now

1 Like

So two things:

  1. when you post code blocks you should put them in between triple backticks : `
    This makes them display nicely. You’ll be able to see this in my second point about your if statement.

  2. Your if statement is poorly formed. if(obj) is not what you want to check. That amounts to “If this object exists” What you want to have there is something that checks if the Object Has that Own Property and the return it if it does:

if( Does object have the property )
    return TheObjects[Property]
else
    return "“Not Found”

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

thank you so much @jwilkins.oboe and @damianicely