Profile Look Up using for loop and if condition
A lookUpProfile function that takes a name and property (prop) as arguments has been pre-written for me.
If both are true, then return the "value" of that property.
If the name does not correspond to any contacts then return the string No such contact.
If prop does not correspond to any valid properties of contact found to match name then return the string No such property.
why it is not working if I use && instead of nested if statement
// 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 (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else return "No such contact";
}
return "No such contact";
// Only change code above this line
}
lookUpProfile("Akira", "likes");
function lookUpProfile(name, prop) {
for (let x = 0; x < contacts.length; x++) {
if (contacts[x].firstName === name) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
}
function lookUpProfile(name, prop) {
// Only change code below this line
for (let i = 0; i <= contacts.length; i++) {
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else return "No such contact";
}
return "No such contact";
// Only change code above this line
}
This function will always return "No such contact" if contacts[0] is not the value you want to get.
Below code will work.
function lookUpProfile(name, prop) {
// Only change code below this line
for (let i = 0; i <= contacts.length; i++) {
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
}
return "No such contact";
// Only change code above this line
}