# Basic JavaScript - Profile Lookup

**URL:** <https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [September 28, 2022, 3:17pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408 "2022-09-28T15:17:34Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 28, 2022, 7:15pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/21 "2022-09-28T19:15:20Z")

</div>

That’s almost the command to check for the property. You’re thinking of hasOwnProperty.

Careful with dot notation - it only works for the exact literal property name, but you have a variable holding the property name.

---

<div class="post-metadata">

**Author:** ![mcmichaeltyler93](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mcmichaeltyler93/32/182800_2.png) [@mcmichaeltyler93](https://forum.freecodecamp.org/u/mcmichaeltyler93)\
**Post date:** [September 28, 2022, 7:18pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/22 "2022-09-28T19:18:32Z")

</div>

Ooooh yeahhhh!!! that’s right! but, doesn’t it have to be dot notation since we are using hasOwnProperty?

```auto
for (let i = 0; i < contacts.length; i++)
  if (contacts[i].firstName === name) 
  if (contacts[i].hasOwnProperty(prop))

```

or can we do

```auto
for (let i = 0; i < contacts.length; i++)
  if (contacts[i].firstName === name) 
  if (contacts[i].hasOwnProperty[prop])

```

---

<div class="post-metadata">

**Author:** ![kwame.arms](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kwame.arms/32/281044_2.png) [@kwame.arms](https://forum.freecodecamp.org/u/kwame.arms)\
**Post date:** [September 28, 2022, 7:25pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/23 "2022-09-28T19:25:06Z")

</div>

I struggled through this one as well, so I wont pretend to have a firm grasp on how it all worked out. I think I was 75% there before I needed a lifeline. You’re getting along better than me, just keep in mind it is difficult. I made progress sometimes after a nights sleep…mulling over the solutions in my mind unconsciously for hours.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 28, 2022, 7:26pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/24 "2022-09-28T19:26:00Z")

</div>

> [@mcmichaeltyler93](#):
>
> `if (contacts[i].hasOwnProperty(prop))`

This part is good.

> [@mcmichaeltyler93](#):
>
> `contacts.likes`

This part will be a problem

---

<div class="post-metadata">

**Author:** ![mcmichaeltyler93](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mcmichaeltyler93/32/182800_2.png) [@mcmichaeltyler93](https://forum.freecodecamp.org/u/mcmichaeltyler93)\
**Post date:** [September 28, 2022, 7:26pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/25 "2022-09-28T19:26:15Z")

</div>

```auto
function lookUpProfile(name, prop) {
  // Only change code below this line
  for (let i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName === name) {
    if (contacts[i].hasOwnProperty[prop]) {
      return contacts[i][prop]
    }
    }
  // Only change code above this line
}
}

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 28, 2022, 7:27pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/26 "2022-09-28T19:27:36Z")

</div>

Cool, that covers a lot of the logic you need. What are the missing possibilities?

---

<div class="post-metadata">

**Author:** ![mcmichaeltyler93](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mcmichaeltyler93/32/182800_2.png) [@mcmichaeltyler93](https://forum.freecodecamp.org/u/mcmichaeltyler93)\
**Post date:** [September 28, 2022, 7:27pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/27 "2022-09-28T19:27:59Z")

</div>

ah wait

```auto
function lookUpProfile(name, prop) {
  // Only change code below this line
  for (let i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName === name) {
    if (contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop]
    }
    }
  // Only change code above this line
}
}

```

---

<div class="post-metadata">

**Author:** ![mcmichaeltyler93](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mcmichaeltyler93/32/182800_2.png) [@mcmichaeltyler93](https://forum.freecodecamp.org/u/mcmichaeltyler93)\
**Post date:** [September 28, 2022, 7:41pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/28 "2022-09-28T19:41:28Z")

</div>

> [@mcmichaeltyler93](#):
>
> ```auto
> for (let i = 0; i < contacts.length; i++) {
> if (contacts[i].firstName === name) {
> if (contacts[i].hasOwnProperty(prop)) {
> return contacts[i][prop]
> }
> }
> 
> ```

```auto
// Setup
const 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) {
  // Only change code below this line
for (let i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName === name) {
    if (contacts[i].hasOwnProperty(prop)) 
      return contacts[i][prop]
    } else {
      return 'No such contact' || 'No such property';
    }
    }
  // Only change code above this line
}

lookUpProfile("Akira", "likes");

```

That gives me

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/c/0/5/c05d0176da3c83d8d64bb650fb2546eb92f79d10.png)

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [September 28, 2022, 7:45pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/29 "2022-09-28T19:45:39Z")

</div>

the code you have written what part of the instructions cover?

---

<div class="post-metadata">

**Author:** ![mcmichaeltyler93](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mcmichaeltyler93/32/182800_2.png) [@mcmichaeltyler93](https://forum.freecodecamp.org/u/mcmichaeltyler93)\
**Post date:** [September 28, 2022, 7:48pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/30 "2022-09-28T19:48:13Z")

</div>

> [@ILM](#):
>
> the code you have written what part of the instructions cover

I’m trying to get my code to say

```auto
if (contacts[i].firstName === name) {
    if (contacts[i].hasOwnProperty(prop))

```

return contacts[i][prop]

else return no such contact or no such property

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 28, 2022, 7:49pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/31 "2022-09-28T19:49:09Z")

</div>

Pick on of those at a time.

Do you want to handle “No such contact” or “No such property” first?

---

<div class="post-metadata">

**Author:** ![mcmichaeltyler93](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mcmichaeltyler93/32/182800_2.png) [@mcmichaeltyler93](https://forum.freecodecamp.org/u/mcmichaeltyler93)\
**Post date:** [September 28, 2022, 7:49pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/32 "2022-09-28T19:49:57Z")

</div>

> [@JeremyLT](#):
>
> No such contact

No such contact I guess.

---

<div class="post-metadata">

**Author:** ![mcmichaeltyler93](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mcmichaeltyler93/32/182800_2.png) [@mcmichaeltyler93](https://forum.freecodecamp.org/u/mcmichaeltyler93)\
**Post date:** [September 28, 2022, 8:08pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/33 "2022-09-28T20:08:25Z")

</div>

Ahhh I’m SO close!!

```auto
function lookUpProfile(name, prop) {

for (let i=0; i<contacts.length; i++) {
  if (contacts[i][prop] === undefined) {
    return 'No such contact'
  }
  if (contacts[i].firstName === name) {
    if (contacts[i].hasOwnProperty(prop))
      return contacts[i][prop];
    } 
    }
  // Only change code above this line
}

```

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/6/e/9/6e9d277fa6ffece33b5e5cff23242ee591f1db87.png)  
But don’t know why lookUpProfile(“Bob”, “number”) didn’t return No such contact

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [September 28, 2022, 8:12pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/34 "2022-09-28T20:12:30Z")

</div>

> [@mcmichaeltyler93](#):
>
> ```auto
> if (contacts[i][prop] === undefined) {
> return 'No such contact'
> }
> 
> ```

> [@mcmichaeltyler93](#):
>
> ```auto
> if (contacts[i][prop] === undefined) {
> return 'No such contact'
> }
> 
> ```

why doesn this makes you say “No such contact”?

---

<div class="post-metadata">

**Author:** ![mcmichaeltyler93](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mcmichaeltyler93/32/182800_2.png) [@mcmichaeltyler93](https://forum.freecodecamp.org/u/mcmichaeltyler93)\
**Post date:** [September 28, 2022, 8:13pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/35 "2022-09-28T20:13:59Z")

</div>

I have no idea ): I’m trying to think super hard.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [September 28, 2022, 8:14pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/36 "2022-09-28T20:14:36Z")

</div>

Delete that. do “No such property” first, it’s easier.

---

<div class="post-metadata">

**Author:** ![mcmichaeltyler93](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mcmichaeltyler93/32/182800_2.png) [@mcmichaeltyler93](https://forum.freecodecamp.org/u/mcmichaeltyler93)\
**Post date:** [September 28, 2022, 8:18pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/37 "2022-09-28T20:18:03Z")

</div>

I’m trying to understand how you knew that lol. What did you look for that told you that? I’m trying to think like that, but to no avail.

This is my code now: Since I have a nested if statement, it’s kind of throwing me off. If I make another if line, it doesn’t work.

```auto
function lookUpProfile(name, prop) {

for (let i=0; i<contacts.length; i++) {
  if (contacts[i][prop] === undefined) {
    return 'No such property';
  }
  if (contacts[i].firstName === name) {
    if (contacts[i].hasOwnProperty(prop))
      return contacts[i][prop];
    } 
    }
  // Only change code above this line
}

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 28, 2022, 8:20pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/38 "2022-09-28T20:20:27Z")

</div>

Step back and look at the big picture for a second.

You want to find the matching contact by checking the name, right?

And you need to check the matching contact for the property, right?

---

<div class="post-metadata">

**Author:** ![mcmichaeltyler93](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mcmichaeltyler93/32/182800_2.png) [@mcmichaeltyler93](https://forum.freecodecamp.org/u/mcmichaeltyler93)\
**Post date:** [September 28, 2022, 8:21pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/39 "2022-09-28T20:21:49Z")

</div>

to check name, you do contact.firstName, no? That’s how I would check what the first name is

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 28, 2022, 8:34pm UTC](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408/40 "2022-09-28T20:34:03Z")

</div>

Syntax follows from the logic you want to express.

You have good syntax for expressing the logic of finding a matching name. I’m trying to prompt you to describe the logic you need for the property before you start guessing syntax. The logic needs to come before the syntax.

[Previous page](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408.md?page=1)

[Next page](https://forum.freecodecamp.org/t/basic-javascript-profile-lookup/558408.md?page=3)
