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;
  }
  if(contacts[i].hasOwnProperty(prop)){
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…

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

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: )