For some reason, the below code is not passing tests. What am I missing?
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)){
console.log(contacts[i][prop])
return contacts[i][prop]
}
else if(contacts[i].firstName!==name){
console.log("here1")
return "No such contact"
}
else if(contacts[i].hasOwnProperty(prop)==false){
console.log("here2")
return "No such property"
}
}
}
lookUpProfile("Sherlock", "likes")
Before you write any code, you need to think about its logic. You see, in the instruction it says to check the whole contacts array before outputting “No such contact” or “No such property”. In your code, it checks one object at a time.
I’ve completed this task. It took me a while, but there’s a solution. You just need to use the “filter()” function to check for name and prop. Do it separately, not in one go. If everything’s okay, use the for loop and your first “if” condition to output the result.
The important thing to note is that return immediately stops your function. You should only return this phrase when you are absolutely sure every contact does not match.
One tip I have is that checking the exceptions first usually cleans up your code.
For example in your original post’s if/else chain, you’re making the same checks twice.
if(contacts[i]["firstName"]===name&&contacts[i].hasOwnProperty(prop)){
console.log(contacts[i][prop])
return contacts[i][prop]
}
// Checking the name a second time
else if(contacts[i].firstName!==name){
console.log("here1")
return "No such contact"
}
// Checking the prop a second time
else if(contacts[i].hasOwnProperty(prop)==false){
console.log("here2")
return "No such property"
}
To remove those redundant checks, you could rewrite it in this order:
if (contacts[i].firstName !== name) {
return "No such contact";
}
if (!contacts[i].hasOwnProperty(prop)) {
return "No such property";
}
// Since we got past the above checks,
// we know the contact is a match and
// the prop exists, so no need to check again.
return contacts[i][prop];
(to be clear, I’m aware that the above is not the solution to the problem, I’m just using your original code as an example)
There is one big thing that you can do. The loop represents checking all contacts. So where, relative to the loop, should the ‘No such contact’ message go? The options are before, after, or inside.
Yes, I was able to solve the problem. From what I can gather is that my function was incorrectly constructed (by me ) So the first name is “Akira” in the contacts and I was passing “Sherlock” to the function. At i=0 the firstName is Akira, and since they are not equal, my function passes the first if and proceeds to the second if.
The big hint is as the answerers suggested to check all names before returning a value for me.
It should be after, when no contacts with the same firstName is found.
My questions was more general though. I write Groovy scripts for my job regularly and I didn’t expect from myself to make such a mistake honestly. So my question is how can I improve in general the way I construct the logic of my code for any coding problem?