function checkObj(obj, checkProp) {
// Only change code below this line
function checkObj(obj, checkProp) {
if (obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return "Not Found";
}
}
// Only change code above this line
}
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36
The issue in your code is that you’re declaring another function inside of the given function. You only need to write the if-else statement
// Only change code below this line
if (obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return "Not Found";
}
// Only change code above this line
Try putting only the if-else statement between the comments like above.
As a side note, you can declare functions inside other functions, you just don’t need it in this challenge
That’s not the issue. you can declare another function inside any function, here as well… It’s that he has declared the same function again. when you declare another function with the same name, it overrides the first function. But in his code, the outer function has no “return” value so it returns undefined.
I think we’re saying 2 different truths that are both applicable in this situation.
By putting the if-else statement inside the inner function, the inner function would be declared but never run. Even if we renamed the inner function, it would never run. If we remove the inner function all together and just have the if-else statement in the outer function, the challenge would pass.
By your explanation the code below should and does work.
function checkObj(obj, checkProp) {
// Only change code below this line
function checkObj(obj, checkProp) {
if (obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return "Not Found";
}
}
return checkObj(obj, checkProp)
// Only change code above this line
}
I just advocated to remove the inner function because it seems simpler
In any case, I’m ok with being wrong if my explanation isn’t correct. If you or someone else more experienced than me disagrees with my logic, please correct my knowledge gap. We’re all trying to learn here including me
The above code works because the outer function has a return value, so it doesn’t return undefined and returns your desired output. But you’re just sort of reinventing the wheel here. you created a function. It does what you want it to do. But you aren’t satisfied. So you nest this function inside another function which does nothing except returning the output of this function. If this function returns undefined then the outer function will also return undefined, if it returns the correct output, the outer function will also return the correct output. The outer function here is redundant. you can remove it.
you don’t necessarily need to write your code within comments. Remember, comments are lines of codes that are ignored by JavaScript( and any programming language for that matter) . Comments are for you and other people to understand what your code is intended to do. you can declare the function within the comments as well.
The inner function ALWAYS runs. It just wasn’t called. so it doesn’t log anything in the console.
when you declare a function within another function, it is called a nested function. A nested function is private to the outer function which means it can’t be accessed outside the outer function. you can only call it inside the outer function. you can read more about nested functions here: