I've been stuck here for a while, and I checked on other solutions which matched mine perfectly. But I can't seem to find the error in my code as it keeps saying..
// running tests
lookUpProfile("Kristian", "lastName") should return the string Vos
lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]
lookUpProfile("Harry", "likes") should return an array
// tests completed
This is the challenge...
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"],
},
];
My code...
function lookUpProfile(name,prop){
for (let i = 0; i < contacts.length; i++){
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
} else if (contacts[i].firstName === name && !contacts[i].hasOwnProperty(prop)){
return "No such property";
}
return "No such contact";
}
// Only change code above this line
}
lookUpProfile("Akira", "likes");
The test parameters
lookUpProfile("Kristian", "lastName") should return the string Vos
lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]
lookUpProfile("Harry", "likes") should return an array
lookUpProfile("Bob", "number") should return the string No such contact
lookUpProfile("Bob", "potato") should return the string No such contact
lookUpProfile("Akira", "address") should return the string No such property
Please I'd be so glad if anyone can help open my eyes.
You were prematurely ending your function if you didn't find the contact in the first iteration. You were close to getting it to work:
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) {
for (let i = 0; i < contacts.length; i++)
{
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop))
{
return contacts[i][prop];
}
else if (contacts[i].firstName === name && !contacts[i].hasOwnProperty(prop))
{
return "No such property";
}
}
return "No such contact";
// Only change code above this line
}
console.log( lookUpProfile("Kristian", "lastName") );
console.log( lookUpProfile("Sherlock", "likes") );
console.log( lookUpProfile("Harry", "likes") );
console.log( lookUpProfile("Bob", "number") );
console.log( lookUpProfile("Bob", "potato") );
console.log( lookUpProfile("Akira", "address") );
I'd recommend that you use Array.prototype.find(), optional chaining (?.) and the nullish coalescing operator (??).
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) {
const profile = contacts?.find(contact => contact.firstName === name);
return profile?.[prop] ?? "No such property";
}
const results = [
lookUpProfile("Kristian", "lastName"), // should return the string Vos
lookUpProfile("Sherlock", "likes"), // should return ["Intriguing Cases", "Violin"]
lookUpProfile("Harry", "likes"), // should return an array
lookUpProfile("Bob", "number"), // should return the string No such contact
lookUpProfile("Bob", "potato"), // should return the string No such contact
lookUpProfile("Akira", "address"), // should return the string No such property
];
results.forEach(result => console.log(result));