Bug in question - does not match solution

Tell us what’s happening:
question ask to modify given code which is short if else statement as per below. But solution requires to build object with different values and even though I build it and tested positively solution does not accept it, therefore you cannot pass this topic.

question asks for:

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

Solution requires below:
Your code so far


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

function checkObj(checkProp) {
// Your Code Here

myObj.hasOwnProperty(checkProp);
return myObj[checkProp] || "Not Found";

}

// Test your code by modifying these values
console.log(checkObj("district"));

Your browser information:

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

Challenge: Testing Objects for Properties

Link to the challenge:

neither is accepted

1 Like

Welcome, lukaszkukla.

Neither of what you have provided should pass the tests.

  1. You are not doing what was taught in previous lessons about this topic:
    How do a function parameters access the object
    Need HELP in how to access an object property!
    Why would you ever use dot notation over bracket notation?

  2. Why are you adding an object myObj?

Click this link, only if you want to see the solution: freeCodeCamp Challenge Guide: Testing Objects for Properties

2 Likes

You do not want to just copy the code from the video. You want to think about the discussion in the video and apply it to the challenge.

Hi JeremyLT,
Thanks for the reply. I am not sure what video you are referring to though?

the task is to " Modify the function checkObj to test if an object passed to the function ( obj ) contains a specific property ( checkProp ). If the property is found, return that property’s value. If not, return "Not Found" ." and the starting code is:

function checkObj(obj, checkProp) {

// Only change code below this line

return “Change Me!”;

// Only change code above this line

}

So my response code to this solution is:

function checkObj(obj, checkProp) {
if(obj.hasOwnProperty(checkProp)) {
return obj.checkProp;
} else {
return “Not Found”;
}
}

but when I click “Run the Tests” I get fail and message below:

// running tests

checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")

should return

"pony"

.

checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")

should return

"kitten"

.

checkObj({city: "Seattle"}, "city")

should return

"Seattle"

. // tests completed // console output

That does not correspond in any way to what I am asked to do, hence my post as I had to omit this question.

P.S.
Copied code from solutions found in “Hints” to test and this gives exactly same error.

5 Likes

Hi Sky020,
They heading “My code so far” is the code I copied from “Hints” section to test if it will pass this lesson as this was a code that worked in the past according to the post. It does not work now. See my reply to JeremyLT for more details. It looks to me that there is some bug in this lesson.

You are creating a new variable myObj. This variable happens to be the same as the video hint for the lesson, if I’m not mistaken,

The challenge says nothing about adding a new global variable. The challenge has DO NOT CHANGE comments that you violated:

function checkObj(obj, checkProp) {
  // Only change code below this line
  return "Change Me!";
  // Only change code above this line
}

You want to use the input object obj and only change the code between the comments to check this code for the given property checkProp. At this point, I’d reset your code because you have changed far more than you are supposed to.

1 Like

This is not correct…

This implies that the object obj looks something like this:

let obj = {
  checkProp: value,
  someKey: someValue
}

But checkProp is not a key. It is a variable name. and this object:

{gift: "pony", pet: "kitten", bed: "sleigh"}

Does not have a key equal to "checkProp"

My first post and first link explains this.

3 Likes

The problem is that

return obj.checkProp;

does not work. I found out that it only works as

return obj[checkProp];

I don’t know why, but I do know this solution is wrong. An FYI for any other unfortunate souls stuck on this one.

14 Likes

The solution does not tell you to use dot notation. Bracket notation is required because checkProp is a variable.

1 Like

Thank you for this. It was driving me crazy.

The confusing part of this problem is that you need to create an object outside of the function but the comments in the pre-formatted code tell us not to code anywhere other than between the comments. In addition, you need to call the function, which again is outside of the comments.

Maybe those comments should be removed or moved.

3 Likes

You do not need to create an object outside of the function to pass this challenge, and you do not need to call the function to pass this challenge.

To verify this, you can copy the hint solution or remove the object and function call from your own solution.

2 Likes

Many thanks! Figured it out based on your feedback.

1 Like

dot notation is not working for this problem

function checkObj(obj, checkProp) {
  // Only change code below this line
  if(obj.hasOwnProperty(checkProp)){
    return obj.checkProp;
  }
  return "Not Found";
  // Only change code above this line
}
1 Like

@diwakar644, as has been explained a few times in this thread, dot notation will not work for this problem.

1 Like

Hi @JeremyLT ,
can you explain more about this? why bracket notation is required, and what is the relation of ‘variable’ with the dot notation and bracket notation. it will be good if you provide some resources.
Thanks

Sure.

In order to use Dot Notation, you have to know the exact name of the property. Take a look at this interactive repl link to see this:

https://repl.it/repls/ThreadbareEmbarrassedPatterns

What’s going on is explained in the MDN.

With Dot Notation,

In the object.property syntax, the property must be a valid JavaScript identifier.

While with Bracket Notation,

In the object[property_name] syntax, the property_name is just a string or Symbol.

function checkObj(obj, checkProp) {

  // Only change code below this line

  if (obj.hasOwnProperty(checkProp)) {

    return obj[checkProp];

  }

  else {

    return "Not Found";

  }

  // Only change code above this line

} //This is my version of the code hope it helps.

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.