Hello friends !
I’m doing the Profile Lookup from the beta (link: https://beta.freecodecamp.org/en/challenges/basic-javascript/profile-lookup) but i’m having issue regarding this rule:
“Bob”, “potato” should return “No such contact”
English is not my native language but i think there is an error in the ask !
If potato is a property and it doesn’t exist, why does it have to give back “No such contact” ?
Here my code:
var 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) {
//little 'cheat' to pass the exercice
/*
if( (name == "Bob") && (prop == "potato") ) {
// return "No such contact";
}*/
var a, b = false;
for(var i = 0; i < contacts.length; i++) {
if( contacts[i].hasOwnProperty( prop ) ) {
a = true;
if( contacts[i]["firstName"] == name ) {
b = true;
return contacts[i][prop];
}else {
b = false;
}
}else {
a = false;
}
}
if( a == false ) {
return "No such property";
}
if( b == false ) {
return "No such contact";
}
};
console.log( lookUpProfile("Kristian", "lastName") ); //"Vos"
console.log( lookUpProfile("Sherlock", "likes") ); //["Intriguing Cases", "Violin"]
console.log( lookUpProfile("Harry", "likes") ); // an Array
console.log( lookUpProfile("Bob", "number") ); //"No such contact"
console.log( lookUpProfile("Bob", "potato") ); //Should be this from the rule "No such contact" but is "No such property" by logic
console.log( lookUpProfile("Akira", "address") ); //"No such property"
Maybe i don’t fully understand this sentence:
If prop does not correspond to any valid properties of a contact found to match name then return “No such property”
Please, let my understand why i may be wrong and explain it to me. Or please confirm my thought so i can move on with the peace of the mind
Have a good life !