I knew the recommended anwser using “hasOwnProperty” to solve this question; however,
I think my way using switch is workable too. But I can’t pass this task.
I though it can pass all the situation in this question, but it didn’t work. :S
Can someone help to look up where’s the bug in my codes? Thank you so much.
Your code so far
// 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[i]; i++ ) {
if (contacts[i].firstName === name) {
switch (prop) {
case "lastName":
return [contacts.firstName, contacts.lastName]
break;
case "number":
return [contacts.firstName, contacts.number]
break;
case "likes":
return [contacts.firstName, contacts.likes]
break;
default:
return "No such property";}
}
else {
return "No such contact";
}
}
// Only change code above this line
}
console.log(lookUpProfile(name, prop))
lookUpProfile("Akira", "likes");
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36
Challenge: Basic JavaScript - Profile Lookup
Link to the challenge: