Basic JavaScript - Testing Objects for Properties

Tell us what’s happening:
Why does the code below not print a result on the console? I am trying to understand this code.

Your code so far

let test = {
  t: "tt",
  r:"rr",
  q: "qq"
};

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

console.log(checkObj(test, t));

Your browser information:

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

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

t is an object property of the object test in my code. Can you just show me how to fix it, than ill get it!
Do I also need to add a variable t?

Just show me the solution please, tried making the t a string already. Already spent 30 minutes trying to get this to work in console.

Not a helpful answer. I already tried “t” and “tt” as the second argument.

have you tried clicking the “Get a Hint” button?

That’s against the rules. We can’t give you an answer to copy.

Those both work fine for me.

I’ll try to provide you with a hint on this: t is not correct for your second argument. That needs to be something else.

In the line of code you’ve written, t indicates a variable with the name t. Which you haven’t defined in that scope.

The code I am showing you is the solution given by the hint, it passes the questions test. But I like to play around with code and test it. This helps me understand code. I put it into a code editor and added console.log(). And added the object test. That is the only difference. This code passed the test. You would be only showing me how to actually use it in a code editor.

Ok. I’m not here to write code for you.

The fix you described works for me. You need to put quotes around the second argument.

The code you provided in the first post doesn’t match the fix you described here

Can you provide code that shows that not working for you?

1 Like

I tied it again, and it worked this time. I tried this countless times in vs code this morning and kept getting errors.

Thank you, I think I did not save before running the code, when I tried the correct answer in the code editor.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.