Could someone nudge me in the right direction here? I’ve been staring at this problem for 2 days. I’ve changed many things, I’ve reassured myself about every part. I don’t get it. I just can’t think of a reason why it wouldn’t work. I would appreciate it if you wouldn’t just flat out give me the straight answer, but maybe drop small hints until I am able to crack it.
Here’s the instructions.
We have an array of objects representing different people in our contacts lists.
A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you.
The function should check if firstName is an actual contact's firstName and the given property (prop) is a property of that contact.
If both are true, then return the "value" of that property.
Here’s the code.
//Setup
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": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName == firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i].prop;
}
else {
return "whats up";
}
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Kristian", "lastName");
Also, does anyone know of a way to practice these kind of problems? Because I find myself getting stuck at this “searching through arrays” thing. Not the first time. Does getting good at algorithms help?
Thank you.