Initial data:

There is an array of contacts, each element is an object with its own properties. We need to make a function that takes the value of first and compares it with all the firstName array. If there is a match, then you need to output the second specified value.

In the specific case, this is the lastName value of the same object. I already and so, and tried, nothing comes out.

It seems that return contacts[i][prop]; just not working.

As a result, should output: "Vos"

 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(first, prop) { for (var i = 0; i < contacts.length; i++) { if (contacts[i].firstName == first) { document.write('First name has matches<BR>') if (contacts[i].hasOwnProperty(prop)) { document.write('Prop exist<BR>'); return contacts[i][prop]; } return "No such property"; break; } } return "No such contact"; } lookUpProfile("Kristian", "lastName"); document.write(lookUpProfile()); 

    1 answer 1

    Your code is working. You simply lose the Vos value and execute a second time with empty values ​​in this place:

     lookUpProfile("Kristian", "lastName"); document.write(lookUpProfile()); 

    Change these two lines to:

     document.write(lookUpProfile("Kristian", "lastName")); 

     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(first, prop) { for (var i = 0; i < contacts.length; i++) { if (contacts[i].firstName == first) { document.write('First name has matches<BR>') if (contacts[i].hasOwnProperty(prop)) { document.write('Prop exist<BR>'); return contacts[i][prop]; } return "No such property"; break; } } return "No such contact"; } document.write(lookUpProfile("Kristian", "lastName")); 

    By the way, you can slightly modify your code using .find() :

     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(first, prop) { let contact = contacts.find(function(c) { return c.firstName === first; }); if (!contact) { return "No such contact"; } if (!contact.hasOwnProperty(prop)) { return "No such property"; } return contact[prop]; } document.write(lookUpProfile("Kristian", "lastName")); 

    • Thank you very much. In such a bundle just did not execute the code yet. About the "let" still read, all ahead. But if with "And" does not match the condition of the problem. After all, you need to display "No such property" if the property does not exist. - Amigo9876
    • @ Amigo9876 are you talking about the second option? I corrected. - MihailPw