Tell us what’s happening:
This code is recommended by many in “Get Hint” section and official video help explanation,
but the code didn’t pass the test when I run it. Got stuck here.
Your code so far
function checkObj(obj, checkProp) {
Hello. In the if condition and also inside the if block, the object should be obj not myObj. What ever object you pass through the function is stored in the obj parameter. Hence you need to use obj to access the object you passed through the function arguments. After correcting this, there is no need for you to use the else if condition here, just use else and return “Not found”.
@sumitsinha215 you can see example objects in the tests, they would all be passed in the function in the parameter obj
(you have the object to manipulate in the function parameter)
yes, there is no object defined because the function should work with any object. the object to test is passed in as function argument, so to try you can have an object and a function call
function checkObj(obj, checkProp) {
// check if obj has the property
}
const testObj = {
"mood": "happy",
"age": "99"
}
checkObj(testObj, "mood"); // should return "happy"
checkObj(testObj, "style"); // should return "Not Found"
checkObj({"dessert": "tiramisù"}, "dessert"); // should return "tiramisù"
any object can be passed in as function argument and the function should work with any object, so you must use the function parameters to reference the passed in object and the string inside the function
function sum(arr, n) {
// Only change code below this line
if (n >= 0) {
return arr[0];
} else {
return sum(arr, n - 1) + arr[n-1];
}
}
// Only change code above this line
sum([2,3,4],1);
sum([2,3,4,5],3);
sum([1],0);
I just need a little help with this code on the same topic. I hope I am posting this in the right forum. It’s the same topic. Here’s my code:
function checkObj(obj, checkProp) {
// Only change code below this line
var myObj = {
gift:"pony",
pet: "kitten",
bed: "sleigh",
city:"Seattle"
}
if (myObj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return "Not Found";
}
checkObj()
// Only change code above this line
}
can you give me some pointers on where I went wrong
It’s fine, but don’t be afraid to create a new topic. And make sure to give an explanation of what is wrong with your code Also, can you give the link to the challenge?
I figured it out. But I’m not understanding how come I can’t get it to print /render to the console? If I call the function wrapped in console.log as such:
console.log(checkObj(gift));
It should print to the console, but it doesn’t. I’ve figured out the code… Also thanks for the info on being able to create a new topic.