Seems like my code should work but i dont think the has own property statements are working correctly because the tests that involve them fail. Anyone have an idea what is wrong with them?
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 cycle = 0; cycle < contacts.length; cycle++) {
if (name === contacts[cycle].firstName && contacts[cycle].hasOwnProperty(prop) == true) {
return contacts[cycle].prop;
} else if (name !== contacts[cycle].firstname) {
return "No such contact";
}else if (name === contacts[cycle].firstname && contacts[cycle].hasOwnProperty(prop) == false) {
return "No such property";}
}
// Only change code above this line
}
lookUpProfile("Akira", "likes");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0
I removed my returns and checked all the variable but it still doesnt work
let x = 0
for (let cycle = 0; cycle < contacts.length; cycle++) {
if ((contacts[cycle].firstName === name) && (contacts[cycle].hasOwnProperty(prop))) {
x = contacts[cycle][prop];
} else if (name !== contacts[cycle].firstName) {
x = "No such contact";
}else if ((contacts[cycle].firstName === name) && (contacts[cycle].hasOwnProperty(prop)) == false) {
x = "No such property";}
} return x
// Only change code above this line
}
You were right i got rid of the last if statement and put the return outside of the loop and it works but i dont understand why it was stopping the loop it seems that the return for the last statement would only be executed if the name was not found so why would it affect the outcome of the first if statement?
Yeah i see. I guess putting that statement in was completely unnecessary anyway but is there a way to write an if statement that would use the return only if the name wasnt found. Also i guess it has to loop through one time before it does anything ? Because the first test was a name that was the first on the list of the object. Thanks for the help tho i wrote the code in a few minutes then spent hours trying to figure out what was going wrong.