Can anyone tell me what’s wrong with my code please?
**Your code so far**
function checkObj(obj, checkProp) {
// Only change code below this line
result =""
var obj = {
gift: "pony",
pet: "kitten",
bed: "sleigh",
city: "Seattle",
}
if(obj.hasOwnProperty("checkProp")){
result = "checkProp"
}
else {
result = "Not Found"
}
return result
// Only change code above this line
}
checkObj("pony", )
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68.
I cant see where I am going wrong. I played around with it a bit but I did get far in one go. And although its not correct, I had a an inkling how to approach it.
But I dont get obj as a paramatar and variable or object.
**Your code so far**
function checkObj(obj, checkProp) {
// Only change code below this line
var result = ""
var Obj = {
"gift": "pony",
"pet": "kitten",
"bed": "sleigh",
"city": "Seattle",
}
if(obj.hasOwnProperty("checkProp")){
result = Obj[checkProp]
}
else {
result = "Not Found"
}
return result
// Only change code above this line
}
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68.
So I got rid of the the var result = “”, however I thought it would have worked and I could have stored the data the depending on the result of the condition. But I could only get 3 right this way and they were all if the condition was false.
Do you have any idea why I could not have use var result = “”. Also
now I have changed and used two return statements and I 'm getting all but one correct. Any idea whats going on?
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
function checkObj(checkProp) {
result =""
var obj = {
gift: "pony",
pet: "kitten",
bed: "sleigh",
city: "Seattle"
}
if(obj.hasOwnProperty(checkProp)){
result = "Yes !"+checkProp+" property exists"
}
else {
result = "Not Found"
}
return result
// Only change code above this line
}
let checkProperty=checkObj("gift" );
console.log(checkProperty);
I originally blurred this out without checking if it was the correct answer.
But if you run it there are errors in the console.
Plus, you changed the function parameters. function checkObj(obj, checkProp)
We don’t advise forum users to post their answers.
It is best to guide the OP to the correct answer.
I understand your confusion when you are starting out. Think of obj as some data that you want to access from a database on a server. The data is already there and your job is to use a function to pull it and display it in the console.
Think of it like if the object had over 100 items or more will it be reasonable for you to redefine it?