Not sure what’s incorrect

Hello, I’m confused as to why my code is not passing the 3rd test. I successfully pass test 1 and 2 but the third test I’m receiving the same error. passing true when it should be false. my code is similar but I’m not sure what’s incorrect about it. Any insight would be great!

has(object, key) { if ( object.key === 'undefined') { return false; } else { return true; }; },

what am I missing!?

In JS, undefined is not a string.

after altering the undefined portion I’m still receiving the same error for test 3 which is telling me that a value is returned as true when it should be false.

What if you change up the operator and switch the returns?

if (... !== undefined)

Something else to consider, does object have a literal property, 'key'? If not, then we cannot use dot notation.

I did initially switch to the does not equal and still got the same error. If I’m understanding you correctly, key is not defined within object so I may have to do something like ` if (object[key] === undefined)"

Edit: this was the error and proved to be the solution;
upon changing object.key → object[key] ; my code passes all tests.

1 Like

That should now work.

Aside

One rarely likes to see return false in the true branch of an if statement.

if (object[key] !== undefined) {
  return true
} else {
  return false
}
1 Like

Ahh I see. I’m extremely literal so when I worked through the ideate portion of the problem I used false first. Changing object.key to object[key] actually fixed the error I was receiving in the 3rd test.

Something else to consider: The returns are boolean literals. What is the outcome of the condition? A boolean. So in fact, we don’t even need the if statement.

return object[key] !== undefined

We’ll still returning a boolean.

Another way to get a boolean result is with the in operator…

return key in object

Of course, this is a step above the naive level of a beginner, but not hard to grasp once we get some practice under our belt.

2 Likes