Hi The question is for 8th excercise from the end in /JS/Basic JS.
//Setup
/*The function should check if name is an actual contact's firstName and
the given property (prop) is a property of that contact.
If both are true, then return the "value" of that property.
If name does not correspond to any contacts then return "No such contact".
If prop does not correspond to any valid properties of a contact
found to match name then return "No such property"*/
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){
// Only change code below this line
var index=0;
var i = 0;
while (contacts[i].firstName != name){
i++;
index++;
}
if (contacts[index].firstName === name && contacts[index].hasOwnProperty(prop)){
return contacts[index][prop];
}
else if (contacts[index].firstName !== name){
return "No such contact";
}
else if (contacts[index].hasOwnProperty(prop) === false){
return "No such property";
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
This code (look above) passed all tests ecxept a cases, where function should return âNo such contactâ. Console write next message:
// running tests
Cannot read property 'firstName' of undefined
Cannot read property 'firstName' of undefined
// tests completed
I suppose the problem is in condition in the first if else.
Tell me, pls, what is wrong?
P.S. I know, that my code can be improved. But the point of this topic is why there is an error.