function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop]
} else if (!contacts[i].hasOwnProperty(prop)) {
return "No such property"
}
if(name !== contacts[i].firstName) {
return "No such contact"
}
}
// Only change code above this line
}
on the 4th checkmark "Bob", "number" should return âNo such contactâ, i am getting âno such propertyâ everytime.
but when i change the code to this ( which nests the property checking condition then it works!
if (firstName === contacts[i].firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return âNo such propertyâ;
}
so my question is this, why does the first one not work correctly and the second block with nested conditions does? Whats the difference in the outcome from the logic?? someone please explain im stuck thank you
Iâve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
Please use the âpreformatted textâ tool in the editor (</>) to add backticks around text.
Thank you so much for the prompt reply.
Ok, here is my confusion i guess (sorry im still new to programatic thinking so proabbly why i failed here). When i see the sentence âThe function should check if name is an actual contactâs firstName and the given property (prop) is a property of that contact.â I immediately think of the && in between two expressions which is why i set up the first block like that. I dont think of nesting one condition in the other like the second code block , but apparently thats the correct way to do it since it works and mines doesnt. I know that the first condtion will fail since âpotatoâ isnt a valid property, but then the program continues to the else if and thats also not true. so then shouldnt it continue the program flow and check the last condition for âno such contactâ? or is the program done running after the last return statement which causes it to stop and not check taht last block? is that my problem?
ok so the first 3 scenarios are all possible outcomes of the logic, i follow taht so far. can you elaborate on the âa bit more verboseâ of the if else test? what am i missing if anything?
@JeremyLT i understand taht you will not write the code for me I apologize that wasnt my intention. So i changed my else if to have the ânotâ property check. it works now but it seems to look very convoluted.
function lookUpProfile(name, prop){
// Only change code below this line
for (let i = 0; i < contacts.length; i++) {
if (name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop]
} else if (name === contacts[i].firstName && !contacts[i].hasOwnProperty(prop)) {
return "No such property"
}
}
return "No such contact"
// Only change code above this line
}
console.log(lookUpProfile("Kristian", "lastName"));
Exactly! And now you can see that the name === contacts[i].firstName is the same between those first two, which youâll see solutions where people rewrite the code as in your original post.
Oh! By ârewrite the codeâ you mean that they would also initially start out with the && expressions like my conditions? And then rewrite it in the nested conditions format?
@JeremyLT When I saw the challenge sentence â If name does not correspond to any contacts then return "No such contact" .â I didnt think to create my else if condition based on that logic. I should have shouldnt i?
Oh i think i see what you mean now with the contacts[i].firstName being the same in both of my conditions. So that leads to the writing of the second way you said people rewrite their code with the nested format because its already expressed in the first condition. Is that what youre telling me? like the code that @colinthornton pointed out, almost like a shorthand version of my code?
Wow thats really smart ! I hope i can start to think/see things like that in my coding. Also, i noticed that the âno such propertyâ is only inside of the nested test for the prop and not inside of the outter test, that just popped out to me âŚ