Hello World!
I’m a newbie, so please bear with me for this.
I’m really struggling with JS. Things seem to have gone from 0 to 100 pretty fast.
After viewing the video and after reading a few posts about this problem (which I’d never be able to solve this without), I reached this implementation.
It works.
The thing is: can it be more optimized? And why was the for loop necessary to begin with?
// Setup
const 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(name, prop) {
// Only change code below this line
for (let i = 0; i < contacts.length; i++)
{
if (name === contacts[i].firstName)
{
if (contacts[i].hasOwnProperty(prop))
{
return contacts[i][prop];
}
else
{
return "No such property";
}
}
}
return "No such contact";
// Only change code above this line
}
lookUpProfile("Akira", "likes");