Profile Lookup- Is my code messy? [SOLVED]

I just finished this problem afters spending all day on it, and I wanted to know if I solved this in a good way. I know the code works, but I feel like I wrote way more code than was necessary, and would like some input from someone more experienced.

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(firstName, prop){
// Only change code below this line
var name = "";
var hasProperty = false;
for( var i = 0; i < contacts.length; i++) {
  if(contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
  if(contacts[i].firstName === firstName) {
   name = firstName;
hasProperty = true;

  if(name !== firstName) {
  console.log("No such contact");
  return "No such contact";
  if(hasProperty === false) {
return "No such property";
// Only change code above this line

// Change these values to test your function
lookUpProfile("jake", "funny");

In short, the answer to your topic question is: yes. Take a gander thru the forum, there are a couple relatively recent topics covering that particular exercise – read those and review your solution.

1 Like

Thanks! Will do!
Now, I am just typing to fulfill the 20 characters rules so I can post this comment…

You have a lot of extra if statements which are not necessary. Below is a very simple algorithm for this function. See if you can take it and make your code in less steps.

Step 1 ) Start at the first object in the array called contacts to begin looping through it.

Step 2) Is the current object’s firstName property equal to name argument? If no, then move to the next object in contacts array and repeat this step, otherwise go to Step 3

Step 3) Does the current object have a property called prop? If so return the property value, otherwise return “No such property”. Go to Step 4.

Step 4) After looping through all objects in contacts, return “No such contact”, because something would already have been returned in the loop if there was a contact.

With the above logic, you could code a solution in 4-6 lines of code.

1 Like

Thank you, thank you, thank you! That was much shorter!

Let me know if you get it to work with this algorithm or if you have other questions.

It worked beautifully!! I did it with a while loop and a for loop, and have a much better understanding of the process now. Thanks so much!

(I included my code earlier, but decided to delete it because it might keep someone else from learning by doing. Wouldn’t want to rob someone of all the great info I just gained. :slight_smile: )