Basic JavaScript - Profile Lookup - Code assumes no repeated firstName values

Tell us what’s happening:

I was struggling with understanding why the return "No such contact"; statement needed to be placed outside of the for loop until someone suggested adding console.log(i) right after the for loop declaration, as a debugging tool.

If, for example, the above return statement is placed inside the for loop, and the function is invoked with the following arguments: lookUpProfile("Akira", "likes"), the output is 0 [ 'Pizza', 'Coding', 'Brownie Points' ] as expected. The zero before the array is the output of console.log(i), and stands for the first index, or first run of the loop.

There is a problem, though, this only works because Akira happens to be a property value of the first item of the contacts index. If the function is now invoked with the following arguments lookUpProfile("Sherlock", "likes") the output becomes 0 No such contact. Thus the need for placing the return statement outside the for loop becomes apparent: to prevent the loop from stopping after a first run. With the return statement in the correct place, the output for the second function call is 0 1 2 3 [ 'Intriguing Cases', 'Violin' ] as expected, since Sherlock is a property value for the fourth item in my array.

This realization led to another one: The logic behind the code only works if the firstName value is unique for each object in the array, since the loop stops right after a first match is found. I modified my contacts array and added a second Akira (second item). This new object only shares the firstName value with the original, and is completely ignored by a function call: lookUpProfile("Akira", "likes"), output: 0 [ 'Pizza', 'Coding', 'Brownie Points' ]. Is there a way to modify the code to account for cases when objects share the firstName value?

Your code so far

const contacts = [
    {
      firstName: "Akira",
      lastName: "Laine",
      number: "0543236543",
      likes: ["Pizza", "Coding", "Brownie Points"],
    },
// Added second item with the same firstName value
    {
      firstName: "Akira",
      lastName: "Kurosawa",
      number: "9999999999",
      likes: ["Movies", "Painting", "Self Mastery"],
    },
    {
      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) {
    for (let i = 0; i < contacts.length; i++) {
        console.log(i); // Not needed for this project, but good as a debugging tool (activate the return statement at the end of the loop to see why)
        if (contacts[i].firstName === name & contacts[i].hasOwnProperty(prop)) {
            return contacts[i][prop];
        }
        else if (contacts[i].firstName === name & contacts[i].hasOwnProperty(prop) === false) {
            return "No such property";
        }
        //return "No such contact";
    }
    return "No such contact";
  }

  console.log(lookUpProfile("Akira", "likes"));  // 0 [ 'Pizza', 'Coding', 'Brownie Points' ], second Akira is ignored.


Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Profile Lookup

Hi @toweringmallorn, and welcome to the forums!

I would say that this challenge is not representative of how real-world data is analyzed because it’s too simplistic in nature. These early challenges are more to teach you the overall concepts in JS vs. real-world applications because they illustrate how to use certain concepts in JS.

In a more real-world data scenario, you would use a variety of data points to ascertain whether a contact record is a duplicate or not, and take action from there. Often times, you’d start running a program/script/procedure in the background of the OS that will carry out these tasks for you. I’ve written a great many of these types of programs over the past 6 and a half years of my work.

1 Like

@marcusparsons
Thank you for the warm welcome and the prompt reply!

That sounds interesting, running programs at such a deep level. I hope I will one day understand, and perhaps apply, such concepts.

1 Like

Absolutely! Keep at it, and I’m certain you will achieve what you want. Just never stop learning and growing in this field.

Happy Coding! :slight_smile:

1 Like

It doesn’t have to be that complicated. You just need a unique value for each user, like a UUID type ID.

The contacts would be inside a database anyway and you wouldn’t search for non-unique values unless you wanted more than one user returned. Like user birthdays so you can send them all a happy birthday cake emoji :birthday:

If you wanted to bill a user you wouldn’t use their name as the identifier but something unique to that user (again some sort of an ID).

Same code with ids
//The IDs should be proper unique IDs obviously, but this is just an example
const contacts = [
  {
    id: 1,
    firstName: "Akira",
    lastName: "Laine",
    number: "0543236543",
    likes: ["Pizza", "Coding", "Brownie Points"],
  },
  {
    id: 2,
    firstName: "Akira",
    lastName: "Kurosawa",
    number: "9999999999",
    likes: ["Movies", "Painting", "Self Mastery"],
  },
  {
    id: 3,
    firstName: "Harry",
    lastName: "Potter",
    number: "0994372684",
    likes: ["Hogwarts", "Magic", "Hagrid"],
  },
  {
    id: 4,
    firstName: "Sherlock",
    lastName: "Holmes",
    number: "0487345643",
    likes: ["Intriguing Cases", "Violin"],
  },
  {
    id: 5,
    firstName: "Kristian",
    lastName: "Vos",
    likes: ["JavaScript", "Gaming", "Foxes"],
  },
];

function lookUpProfile(id, prop) {
  for (let i = 0; i < contacts.length; i++) {
    if ((contacts[i].id === id) & contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else if (
      (contacts[i].id === id) &
      (contacts[i].hasOwnProperty(prop) === false)
    ) {
      return "No such property";
    }
  }
  return "No such contact";
}

console.log(lookUpProfile(1, "likes")); // ["Pizza", "Coding", "Brownie Points"]
console.log(lookUpProfile(2, "likes")); // ["Movies", "Painting", "Self Mastery"]
console.log(lookUpProfile(5, "number")); // "No such property"
console.log(lookUpProfile(6, "number")); // "No such contact"

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.