Build a Profile Lookup - Build a Profile Lookup

Tell us what’s happening:

I have been bugging fow a while. I try and think of my value being carried along the code but :

Why doesn’t it check for the name (firstName) and return a “no such property” if the name Akira is a valid one ?

If the property is invalid and the name is valid, i don’t get the “no such contact” for the values “Akira”, “address”.

Thank you in advance !

Your code so far

let 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(name, prop){
  let searchedName;
  let propOfContact;
  for (let contact of contacts){
    if(contact.hasOwnProperty(prop) === true && contact.firstName === name){
      searchedName = contact;
      propOfContact = searchedName[prop];
      return propOfContact;
    }
    else if(contact.firstName !== name){
      propOfContact = "No such contact";
    }
    else if(contact.hasOwnProperty(prop) === false && contact.firstName === name){
      propOfContact = "No such property"
    } 
  }
  return propOfContact;
}

let firstname = "Akira";
let prop = "address";

let propIs = lookUpProfile(firstname, prop);


console.log(propIs);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0

Challenge Information:

Build a Profile Lookup - Build a Profile Lookup

Okay, i solved it my own. It’ was going over the whole contacts list and didn’t return once it found the name… Glad that there are no same names :slight_smile: