Code works but codecamp keeps failing me

This post dates to Dec. 2021. The code camp challenge has been updated and the answer provided by the bot no longer works. I have been sitting on this for a while because the object, including properties, was missing from the provided code and challenge explanation. The object values were only revealed after I “fail” tested and even adding the object did not resolve the problem on the code camp website. Thus, I have turned to write the code at codepen.io and, there, it works fine. All the returns are correct. So I am wondering what went wrong or what did I not understand correctly? Any help will be appreciated.

This is the code working on codepen:

var obj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh",
  city: "Seattle"
};            

function checkObj(checkProp) {
if(obj.hasOwnProperty(checkProp)){
   return obj[checkProp];
} else {
return "Not Found"
}
}
  **My browser information:**

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

Challenge: Testing Objects for Properties

Link to the challenge:

First, please provide a link to the challenge itself.
Second, what do you mean you “added” the object because something failed? Did the task include making an object? Propably not. The object is propably supposed to be provided as argument to the function call. If your function doesn’t accept that argument, you will keep failing the tests.

First, thank you for your reply,
The link to the challenge was included in the final section of the post, here you go:

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties

The challenge Name is: Testing Objects for Properties

Second: I am sorry, my explanation was maybe not that great.
Basically, ! tried to solve the challenge as best as I could based on the information provided. Clicking on “Run Test”, I could not progress and looked into the hint section at the bottom of the challenge (under the “get help” button), I found that my code should return properties for an object that was not mentioned in the challenge information before. Thus, I added the object (obj) according to the hint section. But still, it does not work, even though the code is correctly working on codepen. On codecamp clicking on “Run Test” will not let me pass. My guess is that the challenge was updated but not the hint section - and now it is confusing.

You changed the function signature. This is the problem with just copy-pasting old answers.

The solution in the guide is correct, but you copy-pasted someone’s old answer to a previous version of the challenge.

I recommend resetting the challenge and trying to solve it without copying someone else’s answer.

1 Like

sorry Jeremy but this answer does not help me. I delete the obj properties from the function parenthesese because it did not understand why it was there. This was not mentioned in the course before.

This was the original function
function checkObj(obj, checkProp)

I changed it to:
function checkObj(checkProp)

please let me know who does this make a difference?

Also, what should I put in to pass the challange then?

You can’t just delete parts of the challenge you don’t understand. Your code can’t possibly pass if you delete arguments to the function that you don’t like.

With this function signature, you are checking the object obj for the property checkProp. You can’t remove the argument obj or your function can’t work with different objects.

OK. Jeremy, I get that you’re trying to be helpful but your not helping me. So let someone else try.
Thanks for trying though.

There isn’t a lot more anyone can add. You can’t change the existing function declaration. It must be

function checkObj(obj, checkProp) {

P.S. I will also add that it is important you understand why the obj must be passed into the function. Don’t consider this challenge completed until you understand that.

1 Like

Nobody is going to give you the code to copy.

Your current answer is mostly correct, but you copied an answer that deleted the obj argument. You need to put that argument back. Then you need to use that argument instead of the obj that you hard-coded.

Adding 5 characters to the function signature will fix your code. That’s all you need to do to make this pass.

Ok. found the problem.

Just for the record: I did not use old code or copied code. So very rude of you Jeremy to keep suggesting that.

Anyway, non of you were really helpfull because you did not look at the whole challange, you just try to find mistakes in my code. (I amsure their could be many improvements soo… yeahr feel free to do that)

For others struggling: IGNOR the hint section under the “get help” button. You do not have to put in an object (with the properties “present” and value “pony” ect.) . This is missleading.

Regardless of how you feel about us, do you understand why you needed to keep obj as the first argument in the function? As @JeremyLT pointed out, your initial code was correct except that you had removed the obj parameter.

You do not need to declare any additional variables or define any objects inside of your function. You must use the function arguments obj and checkProp.

Note: Old solutions found in replies to this post will not work. This challenge has been edited several times since 2017. Only the solution in this post is up to date.

Direct from the hints.

Ok. Just to quickly clear this up. I do not mean the HINTS in the forum. I mean the hints in the actual challenge. Here is a pic. They have to be ignored.

All happy now or does any of you wanna have a final say? because I am done and moving on. Thanks for all the feedback

Those are all 100% correct… Those are the exact function calls used in the test suite. Those must all be followed for your code to pass.

you have changed the function signature, the function calls all have tw arguments, you have only one parameter, it can’t work like that

2 Likes

Dude that’s not a “hint” section, those are the tests that are run to check if your code fullfills the challenge - this might be the missunderstanding :wink:
The “hint”-thing is a button, which would lead you to another site, including tips and a working solution.

These tests show you how the function will be called. Two arguments, the first one being an object, the second one being a string. Hence your code must be able to take these calls and return the expected results.
checkObj({city:"Seattle"}, "city") means that within your function obj={city:"Seattle"} and checkprop="city".
You have to work with obj and checkprop. The tests determine if your function is correct and they display those big X if you failed a test and thus can use it to figure out where an error might be. But they are not themself any kind of hint, outside of just showing you how the function should perform.

1 Like

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