I solved the quiz this way as showed below. I am wondering if there is any better approach to this problem. I am excited to know how did you solve the quiz.
**Your code so far**
// Setup
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 mis_match_counter = 0;
for (var i = 0; i < contacts.length; i++){
if (contacts[i]["firstName"] === name) {
if (contacts[i].hasOwnProperty(prop) == true) {
return contacts[i][prop];
}
else {
return "No such property";
}
}
else {
mis_match_counter += 1;
}
}
if (mis_match_counter == contacts.length) {
return "No such contact";
}
// Only change code above this line
}
lookUpProfile("Akira", "likes");
function lookUpProfile(name, prop) {
// Only change code below this line
for (const key in contacts) {
if (contacts[key].firstName === name) {
if (prop in contacts[key]) {
return contacts[key][prop];
}
else {
return "No such property";
}
}
}
return "No such contact";
// Only change code above this line
}
It looks pretty good, but you can make it even simpler. You don’t need the mis_match_counter because your return "No such contact" logic is after the loop. That means that it is only possible to reach that point if contacts[i]["firstName"] === name was never true.
function lookUpProfile(name, prop) {
for(let i in contacts) {
if(contacts[i].firstName === name) {
if(contacts[i][prop]) { return contacts[i][prop] }
return "No such property"
}
}
return "No such contact"
}
Because putting it in the if condition forces it to be evaluated as a Boolean value which is either true or false. So:
if (contacts[i][prop]) {
can be thought of as
if (Boolean(contacts[i][prop]) === true) {
In this particular case, prop is a string representing the name of a property in an object. If that name doesn’t exist in the object then contacts[i][prop] returns undefined, which is a “falsy” value. Thus, the if statement becomes:
if (undefined) {
and since undefined is a falsy value, the if condition evaluates to false. Otherwise, if the property does exist then the if condition is replaced with the value stored for that property. So if name is “Kristian” and prop is “lastName” then the if condition would be
if ("Vos") {
And the boolean value of any non-empty string is always true. Thus, the if statement returns either true or false.
Anything in JS can be evaluated as a Boolean. Open up the browser dev tools, go to the Console, and start converting things to Boolean and you’ll always get either true or false. I would also recommend you memorize the values that are falsy and then you know that everything else is “truthy”.
On a side note, while if(contacts[i][prop]) does work in this particular challenge, in the real world you would probably not use this unless you were able to 100% guarantee that if a property exists on the object then its value will never be falsy. Because if we had the following property/value:
"lastName": "",
Then the condition would evaluate to:
if ("") {
Which is a falsy value and thus the function would erroneously return “No such property” when in fact the property “lastName” does exist, it just holds an empty string. So it is probably better to check if the property itself exists in the object (as your original solution does) rather than doing a boolean check on the value of the property in the object.