So I'm going through freecodecamp and I'm solving the problems there, to keep in the loop with the programming and I've stumbled on a snag, and I'm not quite sure what's wrong.
So I have an array of objects called contacts, and I need to create a function called lookUp(firstName, prop)
. The text of the assignment is like this:
The function should check if
firstName
is an actual contact'sfirstName
and the given property (prop
) is a property of that contact.If both are true, then return the "value" of that property.
If
firstName
does not correspond to any contacts then return "No such contact"If
prop
does not correspond to any valid properties then return "No such property"
The code:
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intruiging Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
},
];
function lookUp( firstName, prop ){
for( var i = 0; i < contacts.length; i++ ){
if( contacts[i].firstName == firstName ) {
if( contacts[i].hasOwnProperty( prop ) ) {
return contacts[i].prop;
} else {
return "No such property";
}
} else {
return "No such contact";
}
}
}
// Change these values to test your function
lookUp("Kristian", "lastName");
So I'm looping through the array with for
loop checking each object. In the first if
I check if the firstName
property of that objects equals the function parameter firstName, then if it's true, I check if the object has the property prop
, and I should be able to return it. But it seems that
return contacts[i].prop;
is not working, and I'm kinda lost as to why. I'm sure it's something trivial, but I don't see why. When I go to console, and test
contacts[0].likes
I get out the array ["Pizza", "Coding", "Brownie Points"]
, but in my if that's not working. What am I doing wrong here?
EDIT
Ok so I tried with
function lookUp( firstName, prop ){
for( var i = 0; i < contacts.length; i++ ){
if( contacts[i].firstName == firstName ) {
if( contacts[i].hasOwnProperty( prop ) ) {
return contacts[i][prop];
} else {
return "No such property";
}
} else {
return "No such contact";
}
}
}
But I still get the same error :\
Ok I'm dumb, I exited my for loop too early:
function lookUp( firstName, prop ){
for( var i = 0; i < contacts.length; i++ ){
if( firstName == contacts[i].firstName ) {
if( contacts[i].hasOwnProperty( prop ) ) {
return contacts[i][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
}
This works.
I think you are confused about what return does. Your code will only do one iteration and return "No such contact"
. Return immediately stops the function to execute. Here is a fiddle of what I mean demonstrated with console.log
https://jsfiddle.net/oegw3a4y/
In your situation, the first iteration evaluates to false in the first if
statement and immediately goes to the else
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With