In profile lookup Exercise , I hands on this exercise and I write that code given below.
function lookUpProfile(name, prop) {
// Only change code below this line
for(let i = 0; i < contacts.length; i++) {
if(contacts[i].firstName === name) {
if(contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
return "No such a Property";
}
}
return "No such a contact";
Run this code it shows error in if it is bob, it will return the No Such a contacts and Still I get confuse why it shows an error because, I write if it has name and property it will return that property of the name inside the contacts(array object) else, it will return “No such a property”.
I’ve edited your code 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.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
also here, the text No such a Property and No such a contact is incorrect.
You must use the text provided in the challenge. (I can’t tell you what that is because I don’t have a link to the challenge to check at the moment)
I checked this thread once I managed to get to a solution myself and it seems that I overcomplicated things a bit - I’m just wondering how much of an overkill this is, would this be considered bad code?
function lookUpProfile(name, prop) {
let index = contacts.length - 1; // ?
let noName = 0;
while (index >= 0) {
if (name === contacts[index]["firstName"] && contacts[index].hasOwnProperty(prop)) {
let lookupResult = contacts[index][prop];
return lookupResult;
} else if (name === contacts[index]["firstName"]
&& contacts[index].hasOwnProperty(prop) === false) {
return "No such property";
} else if (name !== contacts[index]["firstName"]) {
noName++;
index--;
}
if (noName === contacts.length) {
return "No such contact";
}
}
lookUpProfile("Akira", "likes");