My answer is rejected by it runs on an online tester

Tell us what’s happening:

After solving the problem, I tested my solution several times, I also checked for the answer in the help video, I improved my solution but even then the platform rejected my answer, I tried testing my solution on on an online JS tester, and it worked (since the command control.log didn’t worked on that website, i changed the return statement to be able to test the output of the program.
here is the code I tested:
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”,
city: “Seattle”
};

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

}

// Test code
checkObj(“gift”);

Your code so far


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 code
checkObj("gift");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15.

Challenge: Testing Objects for Properties

Link to the challenge:

Hey there! Welcome to the FCC community! :smiley:

As far as I can see, you have modified the challenge proposed by the FCC platform.
The function initially has two values passed in it (obj, checkProp). You only use checkProp in yours.

Additionally, the challenge’s object is named “obj”. You named your object myObj.

If the parameters remain the same as the challenge proposed, your solution is treated as correct.

I have returned the object’s name to its original one, removed alert and set the two parameters to be checked by the function, as below:

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

In my opinion, while your solution shows the correct result, it is a bit different than the one proposed by FCC. By passing obj as function parameter, you can check the properties of more than one specific object by using the function, expanding its utility, while your solution only checks one specific object.

Hope it helps at least a bit.

4 Likes

As @Andreea2 said, your function will return the correct values for the tests, but it is not the function that the challenge asked for. Your function only looks for properties in the object that you added. The challenge is to write a function that takes an object and takes a property and looks for the property in that object.

When the code in a challenge says

  // Only change code below this line

you need to follow that instruction.

3 Likes

If the challenge is different than what you see in the video, replacing the code with what you see in the video will cause the challenge to fail. You really need to pay attention to the Only change comments.

1 Like

Thanks @Andreea2, @ArielLeslie, @JeremyLT , your explanation help me to understand the problem with my code.

1 Like