Basic JavaScript: Profile Lookup?

I am struggling with “Bob”, “potato”` should return “No such contact”

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

I don’t know, anybody?

I went to gitHub for assistance. I’m stumped

Can you share your code and explain what parts you are having trouble with?

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

  var msg = 'No such contact';

  for (var p in contacts) {
    if (contacts[p].firstName === firstName && contacts[p].hasOwnProperty(prop)) {
      msg = contacts[p][prop];
    } else if (!contacts[p].hasOwnProperty(prop)) {
      msg = 'No such property';
    }
  }

firstName: Bob 
  return msg;
// Only change code above this line
}

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

I’m basically lost on this topic. I tried to come up with it on my own and didn’t have any success. I resorted going to github and found this to help me understand what they are asking. JavaScript isn’t my strong suit yet.

My issue, is I

"Bob", "potato" should return “No such contact”

How do I make it so Bob returns as “No such contact”

look at your code with this:
http://www.pythontutor.com/javascript.html

look how msg changes

plus that random firstName: Bob there in the middle, what’s that doing there?

1 Like

That line’s giving you trouble. You only want to check ONE condition on that line. If the user is the right one, THEN you want to check if they have that property.

Nested if here.

… also, what is the intent in your having that firstName: Bob just before the return statement? I see fatal errors in that…

1 Like

SnowMonkey, JavaScript is not my strong suit yet. I took a week off from CodeCamp and now i’m basically lost.

Thanks

I’ll take a quick break, and regroup. I’ve been staring at the screen for about 30 min at this point

have you tried python tutor? that really helps me in solving things when I have no idea what’s happening

1 Like

Less about the language, more about the logic. Try this pseudo-code:

  1. loop over each contact in my array.
  2. Does this user’s name match the given name?
    2a. if yes, does this user have the matching property? return the value.
    2b. if no, return “no such property”.
  3. If user’s name doesn’t match, keep looping back to 1.
  4. If we’ve looped over all, then we return “no such contact”.
1 Like

Hi im back looking into it, thanks


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

  var msg = 'No such contact';

  for (var p in contacts) {
    if (contacts[p].firstName === firstName) {
      if (contacts[p].hasOwnProperty(prop)) {
        msg = contacts[p][prop];
      } else if (!contacts[p].hasOwnProperty(prop)) {
        msg = 'No such property';
      }
    }
    
  }

  return msg;
// Only change code above this line
}

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

1 Like

I got some help from my friend on this one, thanks for chiming in!

Glad you figured it out, but it is not necessary and preferred if you do not post the full working solution to prevent spoiling it for others.

Thanks and happy coding!

1 Like

Ah okay, gotcha.

Thanks

I haven’t I’ll look into it, thanks!