What's the detailed explanation for why the 1st function is correct and the 2nd is wrong?

Joined
Sep 12, 2022
Messages
39
Reaction score
0
I understand that there are many easier and shorter ways of solving it but I want to grasp the concept behind the 2nd function in such a scenario. Solutions should be in the 2nd function format only and should be accompanied by a detailed explanation.

The logic
  • I have an array of objects representing different people in a contacts list.
  • 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 the string No such contact.
  • If prop does not correspond to any valid properties of a contact found to match name then return the string No such property.
The object array const contacts = [ { firstName: "Akira", lastName: "Laine", number: "0543236543", likes: ["Pizza", "Coding", "Brownie Points"], }, { firstName: "Harry", lastName: "Potter", number: "0994372684", likes: ["Hogwarts", "Magic", "Hagrid"], } ]


1st function (Correct)

function lookUpProfile(name, prop) {


for (let x = 0; x < contacts.length; x++) { if (contacts[x].firstName === name) { if (contacts[x].hasOwnProperty(prop)) { return contacts[x][prop]; } else { return "No such property"; } } } return "No such contact"; }


lookUpProfile("Akira", "likes");


2nd function (wrong)

function lookUpProfile(name, prop) {


for (let x = 0; x < contacts.length; x++) { if (name === contacts[x].firstName && contacts[x].hasOwnProperty(prop)) { return contacts[x][prop]; } if (name !== contacts[x].firstName || contacts[x].lastName) { return "No such contact"; } if (prop !== contacts[x][prop].name) { return "No such property"; } } }


lookUpProfile("Akira", "likes");
 
Joined
Mar 11, 2022
Messages
227
Reaction score
32
Nobody can read that. Please format your code within code tags. Don't expect efford from us if you're not ready to put effort by yourself in it. At least you need help from us. Not the other way around. Sorry but this has to be said.
 
Joined
Sep 12, 2022
Messages
39
Reaction score
0
I wish to edit the codes above but I can't find the edit button, hence I have rewritten them. Between the time of posting the original question and now, I have figured out that one reason the 2nd function might be faulty is that I am referencing properties that don't exist. i.e, if (prop !== contacts[x][prop].name). If there's a way the 2nd function can be modified, retaining the use of !== operator, kindly share such functional modification.

The object array
JavaScript:
array const contacts = [ { firstName: "Akira", lastName: "Laine", number: "0543236543", likes: ["Pizza", "Coding", "Brownie Points"], }, { firstName: "Harry", lastName: "Potter", number: "0994372684", likes: ["Hogwarts", "Magic", "Hagrid"], } ]

1st function (correct)
JavaScript:
function lookUpProfile(name, prop) {


for (let x = 0; x < contacts.length; x++) { if (contacts[x].firstName === name) { if (contacts[x].hasOwnProperty(prop)) { return contacts[x][prop]; } else { return "No such property"; } } } return "No such contact"; }


lookUpProfile("Akira", "likes");

2nd function (wrong)
JavaScript:
function lookUpProfile(name, prop) {


for (let x = 0; x < contacts.length; x++) { if (name === contacts[x].firstName && contacts[x].hasOwnProperty(prop)) { return contacts[x][prop]; } if (name !== contacts[x].firstName || contacts[x].lastName) { return "No such contact"; } if (prop !== contacts[x][prop].name) { return "No such property"; } } }


lookUpProfile("Akira", "likes");
 
Joined
Jul 3, 2022
Messages
93
Reaction score
23
JavaScript:
function lookUpProfile(name, prop){
           for(let x of contacts){
             if(x.firstName !== name && x.lastName !== name){
               return "No such contact";
             }
             if(!x[prop]){
               return "No such property";
             }
             return x[prop];
            }
          }


         console.log( lookUpProfile("Akira", "likes") ); // ['Pizza', 'Coding', 'Brownie Points']
         console.log( lookUpProfile("Akita", "likes") ); // No such contact
         console.log( lookUpProfile("Akira", "lacks") ); // No such property
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top