Tell us what’s happening:
I was struggling with understanding why the return "No such contact";
statement needed to be placed outside of the for loop until someone suggested adding console.log(i)
right after the for loop declaration, as a debugging tool.
If, for example, the above return statement is placed inside the for loop, and the function is invoked with the following arguments: lookUpProfile("Akira", "likes")
, the output is 0 [ 'Pizza', 'Coding', 'Brownie Points' ]
as expected. The zero before the array is the output of console.log(i)
, and stands for the first index, or first run of the loop.
There is a problem, though, this only works because Akira happens to be a property value of the first item of the contacts index. If the function is now invoked with the following arguments lookUpProfile("Sherlock", "likes")
the output becomes 0 No such contact
. Thus the need for placing the return statement outside the for loop becomes apparent: to prevent the loop from stopping after a first run. With the return statement in the correct place, the output for the second function call is 0 1 2 3 [ 'Intriguing Cases', 'Violin' ]
as expected, since Sherlock is a property value for the fourth item in my array.
This realization led to another one: The logic behind the code only works if the firstName value is unique for each object in the array, since the loop stops right after a first match is found. I modified my contacts array and added a second Akira (second item). This new object only shares the firstName value with the original, and is completely ignored by a function call: lookUpProfile("Akira", "likes")
, output: 0 [ 'Pizza', 'Coding', 'Brownie Points' ]
. Is there a way to modify the code to account for cases when objects share the firstName value?
Your code so far
const contacts = [
{
firstName: "Akira",
lastName: "Laine",
number: "0543236543",
likes: ["Pizza", "Coding", "Brownie Points"],
},
// Added second item with the same firstName value
{
firstName: "Akira",
lastName: "Kurosawa",
number: "9999999999",
likes: ["Movies", "Painting", "Self Mastery"],
},
{
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) {
for (let i = 0; i < contacts.length; i++) {
console.log(i); // Not needed for this project, but good as a debugging tool (activate the return statement at the end of the loop to see why)
if (contacts[i].firstName === name & contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
else if (contacts[i].firstName === name & contacts[i].hasOwnProperty(prop) === false) {
return "No such property";
}
//return "No such contact";
}
return "No such contact";
}
console.log(lookUpProfile("Akira", "likes")); // 0 [ 'Pizza', 'Coding', 'Brownie Points' ], second Akira is ignored.
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Challenge Information:
Basic JavaScript - Profile Lookup