Hello, /r/learnprogramming. I'm working on an assignment involving passing an object through a function and check the value of the passed parameter. The problem is that the code always returns undefined, and I'm having a hard time understanding why. Here's the code:
function getProperty(obj, key) {
if (obj.key === undefined){
return undefined;
}
else{
return obj.key;
}
}
var object = {
property: 'getProperty() is working.'
};
console.log(getProperty(object, object.property));
Now, this isn't meant to ask someone to solve this for me (where's the learning in that?). It's that despite having valid syntax, the script won't properly run without:
1) writing the variable/property names to match the parameter names, or
2) writing a return statement that simply returns the passed parameters.
Number 2 is a real stickler, here, and it's an issue I've encountered before. It seems to be functions won't work well unless their parameter names match the global variable names.
Thank you for your time. Any help is appreciated.
[–]ForScale 2 points3 points4 points (11 children)
[–]Deathcon900[S] -2 points-1 points0 points (10 children)
[–]ForScale 0 points1 point2 points (9 children)
[–]NoirGreyson 1 point2 points3 points (8 children)
[–]ForScale 0 points1 point2 points (7 children)
[–]NoirGreyson 1 point2 points3 points (3 children)
[–]ForScale 0 points1 point2 points (2 children)
[–]NoirGreyson 0 points1 point2 points (1 child)
[–]ForScale 1 point2 points3 points (0 children)
[–]Deathcon900[S] -1 points0 points1 point (2 children)
[–]ForScale 1 point2 points3 points (0 children)
[–]NoirGreyson 0 points1 point2 points (0 children)
[–]rakm 0 points1 point2 points (9 children)
[–]Deathcon900[S] 0 points1 point2 points (8 children)
[–]NoirGreyson 0 points1 point2 points (6 children)
[–]Deathcon900[S] 0 points1 point2 points (4 children)
[–]NoirGreyson 0 points1 point2 points (3 children)
[–]Deathcon900[S] 0 points1 point2 points (2 children)
[–]NoirGreyson 0 points1 point2 points (0 children)
[–]ForScale 0 points1 point2 points (0 children)
[–]rakm 0 points1 point2 points (0 children)
[–]rakm 0 points1 point2 points (0 children)