Basic JavaScript - Profile Lookup

contacts[prop]

?
So I guess,

if the property is undefined, return no such property? 

I already got the property part though so now i’m confused lol

That’s syntax. Talk to me about what you want before guessing syntax.

1 Like

I want to say

If the contacts property is undefined, then return ‘no such property’. AND if the name doesn’t correspond to any contacts, then return ‘no such contact’.

“if the name doesn’t correspond to any contacts” is what’s throwing me off. Don’t know how to write that.

Focus on one at a time here.

Which contact do you want to check here in this phrase?

I want to check the index

The index? Which index?

I thought you were talking about a contact?

Well you said “which contact”, so it’s going to loop through each contact, right? Contact is the object

So if you find any contact in the list without the property you want to say “No such property”? That seems fishy

I’m thinking that contact is the whole object, right?

There are several different contact objects though.

I believe we want to check the firstName contact, if that’s what you mean by contact?

That’s what I’m getting at. Which specific contact do you want to check for the property?

Let’s say I have both Amy and Jeff in my phone. Jeff gave me his phone number and email address. Amy only gave me her phone number. If I look in my phone for Jeff’s email address, should I stop looking for email addresses when I see that Amy doesn’t have one and conclude that Jeff doesn’t have one either?

Well what I’m trying to say is that I will loop through the contacts, and if there’s no property, then I’ll return no such contact

Why?

Finding the contact and checking for the property are two different things.

There are three cases here

  1. Jeff isn’t in my phone book

  2. Jeff is in my phone book but didn’t give me an email address

  3. Jeff is is my phone book and he gave me his email address

You’re mixing up 1) and 2), but your logic for 3) is correct.

I apologise, I’m really lost. I guess I don’t know how to answer Which specific contact do you want to check for the property :disappointed:

The function gives you a name and a property. You’re correctly searching the list of contacts for that name and returning the property if it is there.

You are missing

  1. checking everyone any saying that the person with that name isn’t on your list

  2. finding the person but they don’t have the required property filled out

Your last check for ‘no such property’ doesn’t have any logic for checking that it is the contact you are looking for.

I’ve skimmed through the replies and it looks like you’re struggling to think through how to approach this problem step-by-step (ie, your algorithm). You should have the step-by-step logic figured out, and then translate that logic into syntax.

I’ll start you off by showing you how I’d work through the first test case, as a human being.


lookUpProfile("Kristian", "lastName")

Okay, I need to find Kristian. Let’s look through the contact list from the top.

  {
    firstName: "Akira",
    lastName: "Laine",
    number: "0543236543",
    likes: ["Pizza", "Coding", "Brownie Points"],
  }

Akira, that’s not Kristian. Next…

  {
    firstName: "Harry",
    lastName: "Potter",
    number: "0994372684",
    likes: ["Hogwarts", "Magic", "Hagrid"],
  }

Harry, that’s not Kristian. Next…

  {
    firstName: "Sherlock",
    lastName: "Holmes",
    number: "0487345643",
    likes: ["Intriguing Cases", "Violin"],
  }

Sherlock, that’s not Kristian. Next…

  {
    firstName: "Kristian",
    lastName: "Vos",
    number: "unknown",
    likes: ["JavaScript", "Gaming", "Foxes"],
  }

Okay! I’ve found Kristian.

Now do they have a lastName…

  {
    firstName: "Kristian",
 -> lastName: "Vos",
    number: "unknown",
    likes: ["JavaScript", "Gaming", "Foxes"],
  }

Yep! And its value is "Vos".

So the answer to lookupProfile("Kristian", "lastName") is "Vos".


But there are two more variations you’ll need to work out yourself. The case where the property doesn’t exist on a found contact, and the case where the contact isn’t found in the contact list. Try to think out how you would tackle those cases in a step-by-step manner (in human language) as I showed you above.

Now often when we tell people on here to “explain how you would do this step-by-step, as a human”, people will say “I just scan it and know! I don’t take steps!” But I think they’re wrong, we’re just able to take these steps very quickly and mostly subconsciously. In order to code well, you need to be able to break down these steps as discretely as possible, so that you can translate them into programs.

1 Like

But the lastname example… They are looking for the first name

That’s where I’m confused…I come up with a plan, but I don’t know how to code it. Or, the plan I come up with is ALWAYS wrong…it’s like it’s so abstract that I always end up failing at it. I can speak 5 languages but can’t grasp what you are telling me :disappointed: It’s disappointing

what do you mean with this? try to explain more, to expand