Profile Lookup: Help plz! My work so far

Tell us what’s happening:

What’s working so far: I am able to check if “name” is an actual contact’s first name. I am also able to return the prop for “lastName.”

However, the last name being returned is for the first object (0). I thought by nesting my loops, that the second loop would search within the one that corresponds with the “name” match, but it doesn’t. Do you have any tips/suggestions on how I can get the nested loop to work?

Please don’t tell me the answer and instead give me hints/suggestions.

Thank you!

Your code so far


//Setup
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(name, prop){

for (const test1 in contacts){
let value1 = contacts[test1]["firstName"];
if (name === value1){

 for (const test2 in contacts){
  let value2 = contacts[test2]["lastName"];
  if (prop === "lastName"){return value2};
}
}
}
}

// Change these values to test your function
// lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

so lets start at the beginning:

for(const test1 in contacts){...}

This won’t do what you think it will do. for...in is intended to allow you to iterate through the property names of an object, not iterate over an array. Take a look at https://devdocs.io/javascript/statements/for...in to see what I mean.

Instead, you need to use a mechanism to check each member of the array, and for each member, check if its firstName property is equal to name. There are a number of looping mechanisms you can use for this, but at this point in the curriculum, you’d most likely use a

for(let index=0; index=<your value here>; index++){...}
1 Like

Thanks! Much appreciated.

I was able to get everything working except when it comes to returning the “No such contact” response. I tried adding an “else if” statement and it doesn’t seem to work.

When it comes to the first array (Akira), everything works perfectly. However, I get returns of “No such contact” for other correct names in the follow up arrays (ex. Harry). This leads me to believe that the “else if” statement is not looping/checking through all the other arrays, is the solution a nested array? If so I’m not really sure how to go about doing that.

Is there something I’m overlooking? Again it would be great to get a hint and not the full solution. Thank you.

//Setup
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(name, prop){

 for (var i=0; i < contacts.length; i++){
 if (name === contacts[i]["firstName"] && contacts[i].hasOwnProperty(prop) === true) { 
  return contacts[i][prop];
 } else if (name !== contacts[i]["firstName"]){ return "No such contact"};
 
 }
 }
  

  


// Change these values to test your function
// lookUpProfile("Akira", "likes");

The problem is, you’re trying to combine both “if this name matches AND if this person has the matching property.” Try this logic:

  1. loop over each contact. Does this contactname match?
  2. If yes, does this contact have the requested property?
  3. if yes to 2, return that property.
  4. if no to 2, return "No such property.
  5. if no to 1, loop to the next contact.
  6. if we looped through all the contacts, and none had a matching name…
1 Like

Thanks, I got step 1-4 working, but I’m just getting stuck on how to get the loop to go to the next contact, it just seems to die after going through the first array, I tried nested loops but that wasn’t working, do you have any tips or resources I could look into?

Really appreciate the help so far.

//Setup
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(name, prop){

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

remember that the whole content of the loop is executed for each i
so once i is 0 you have contacts[i] that is the one for Akira, and if name is different you have name === contacts[i]["firstName"] that is false, and you return "No such contact" - maybe it is not the right place for that return statement?

you may find useful to look at your code with this tool:
http://www.pythontutor.com/javascript.html

1 Like

:slight_smile: Thanks all for the help!!! Works now. Much appreciated.

//Setup
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(name, prop){

 for (var i=0; i < contacts.length; i++){
 if (name === contacts[i]["firstName"] === true){
   if (contacts[i].hasOwnProperty(prop) === true){
     return contacts[i][prop];
   } else if (contacts[i].hasOwnProperty(prop) === false){return "No such property"};
 }

 }
   {return "No such contact"}
 }