Testing Objects for Properties has bugs

Tell us what’s happening:
this challenge obviously have a bug. its impossible to solve as two requested answers are contradicting each other.
image

  **Your code so far**

function checkObj(obj, checkProp) {
// Only change code below this line
var myObj = {

pet: “kitten”,
bed: “sleigh”,
city: “Seattle”
};
const hiObj = {
pet: “kitten”,
bed: “sleigh”
}
if (myObj.hasOwnProperty(checkProp)){
return myObj[checkProp]
}else
return “Not Found”;
}
checkObj(“gift”);

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0

Challenge: Testing Objects for Properties

Link to the challenge:

image

if you look at the first test, gift has the value of pony. the last test gift is not in the object being passed. the test should work.

and you should be using the object passed from the function, not ones hard coded into your function. use the parameter obj

3 Likes

There is no bug, and it is supposed to be that way

The challenge requires you to make a function that checks if the passed Object , the first argument/parameter , contains the passed property , the second argument/parameter.

The object in the first test has property gift with value of pony, and the second argument requires the value stored in the “gift” property, thus the return shall be “pony”.

The second test you’ve marked doesn’t have the “gift” property in the passed object so the return shall be not found

Also when putting code in the forum wrap it by three back ticks (`) on a separate lines, one before your code, and one after your code. that makes it easier for reading.

3 Likes

They are not contradicting :wink:
If I ask you to add two numbers, if the numbers are (2, 4) then you should anser “6”, if they are (1,4) you should anser “5”.
Just because 6!=5 doesn’t mean there is a contradiction → those were just completly different tasks.
The second argument might be identical, the first one however isn’t.

2 Likes

image
the only time the last statement marks correct is when “gift”: “pony” is not in the object.
i’ve been on this for almost 2 hours…do you have a solution?

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


if (myObj.hasOwnProperty(checkProp)){
    return myObj[checkProp]
}else
    return "Not Found";
}
checkObj("gift"); ```

Please look at the function again: notice how it takes two arguments: (obj, checkProp)
You are supposed to use this obj → you are using myObj instead.

2 Likes

used obj… got the same result… myObj was the object name …i changed and got the same result.

check the result hen i removed the “gift” property.
image

Yeah, for the test-object to be provided in the function call. Within the function you should only use obj.

The test cases literally show you how the function is called with two arguments.

2 Likes

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