# freeCodeCamp Challenge Guide: Profile Lookup

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [July 7, 2016, 9:05pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259 "2016-07-07T21:05:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [July 7, 2016, 9:05pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/1 "2016-07-07T21:05:34Z")

</div>

# [Profile Lookup](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup)

* * *

## Problem Explanation

- Change the code below `// Only change code below this line` and up to `// Only change code above this line`.
- Ensure that you are editing the inside of the `lookUpProfile()` function.
  - This function includes two parameters, **name** and **prop**.

- The function should look through the **contacts** list for the given **name** parameter.
  - If there is a match found, the function should then look for the given **prop** parameter.
  - If both **name** and the associated **prop** are found, you should return the value of the **prop**.
  - If **name** is found and no associated **prop** is found, you should return `No such property`.

- If **name** isn’t found anywhere, you should return `No such contact`.

* * *

## Hints

### Hint 1

Use a `for` loop to cycle through the **contacts** list.

### Hint 2

Use a nested `if` statement to first check if the **firstName** matches, and then checks `if` the **prop** matches.

### Hint 3

Leave your `return "No such contact"` out of the `for` loop as a final catch-all.

### Hint 4

A function immediately stops at the point where `return` is called.

MDN: [Interrupt a function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return#interrupt_a_function)

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> function lookUpProfile(name, prop) {
> for (let x = 0; x < contacts.length; x++) {
> if (contacts[x].firstName === name) {
> if (contacts[x].hasOwnProperty(prop)) {
> return contacts[x][prop];
> } else {
> return "No such property";
> }
> }
> }
> return "No such contact";
> }
> 
> ```
> 
> #### Code Explanation
> 
> - The `for` loop runs, starting at the first object in the **contacts** list.
> - If the **firstName** parameter passed into the function matches the value of the `"firstName"` key in the first object, the `if` statement passes.
> - Then, we use `.hasOwnProperty()` method (checks if there’s a given property and returns a boolean) with **prop** as an argument. If it’s true, the value of **prop** is returned.
> - If the second `if` statement fails, `No such property` is returned.
> 
> - If the first `if` statement fails, the `for` loop continues on to the next object in the **contacts** list.
> - If the **firstName** parameter isn’t matched by the final **contacts** object, the `for` loop exits and `No such contact` is returned.
> 
> **Example Run**
> 
> - `lookUpProfile("Akira","likes");` runs.
> - `"Akira"` is matched to the `"firstName"` key in the first object, so the `if` statement returns true.
> - `"likes"` is found within the first object, so the second `if` statement returns true.
> - The value of `"likes"` is returned - `"Pizza", "Coding", "Brownie Points"`.

> **Solution 2 (Click to Show/Hide)**
>
> ```javascript
> function lookUpProfile(name, prop) {
> for (let i = 0; i < contacts.length; i++) {
> if (contacts[i].firstName === name) {
> if (prop in contacts[i]) {
> return contacts[i][prop];
> } else {
> return "No such property";
> }
> }
> }
> return "No such contact";
> }
> 
> ```
> 
> #### Code Explanation
> 
> This works as the last example but uses the `in` operator to look for `prop` instead of the `hasOwnProperty()` method.
> 
> #### Relevant Links
> 
> - [“Iterate with JavaScript For Loops” - _fCC’s challenge_](https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops/)
> - [“Object.prototype.hasOwnProperty()” - _MDN JavaScript reference_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)
> - [“in operator” - _MDN JavaScript reference_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in)

---

<div class="post-metadata">

**Author:** ![fahdi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fahdi/32/162917_2.png) [@fahdi](https://forum.freecodecamp.org/u/fahdi)\
**Post date:** [April 12, 2017, 2:20am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/2 "2017-04-12T02:20:24Z")

</div>

I did something like this

```
function lookUpProfile(firstName, prop) {    
    var result = contacts.filter(x => x.firstName == firstName)

    if (result.length === 0) {
        return "No such contact";
    } else {
        return result[0][prop] ? result[0][prop] : "No such property";
    }
}
```

or

```
function lookUpProfile(firstName, prop) {
    let contact = contacts.find((x) => x.firstName === firstName)
    return contact ? contact.hasOwnProperty(prop) ? contact[prop] : "No such property" : "No such contact”;
}
```

---

<div class="post-metadata">

**Author:** ![codeolas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codeolas/32/71457_2.png) [@codeolas](https://forum.freecodecamp.org/u/codeolas)\
**Post date:** [April 20, 2017, 10:01pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/4 "2017-04-20T22:01:23Z")

</div>

The firstName variable inside the lookup function should be changed to anyName, or even just Name.

Edit: Better yet, just use myName and myProp as in the lessons before.

---

<div class="post-metadata">

**Author:** ![JonFieldings](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@JonFieldings](https://forum.freecodecamp.org/u/JonFieldings)\
**Post date:** [June 17, 2017, 1:41am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/6 "2017-06-17T01:41:57Z")

</div>

Man, I really feel like an idiot. I spent a couple of hours trying to figure this out, just to find that one of my return statements was out of place. Here my original code:

for (i = 0; i \< contacts.length; i++) {  
if (firstName === contacts[i].firstName) {  
if (contacts[i].hasOwnProperty(prop)) {  
return contacts[i][prop];  
} else {  
return “No such property”;  
}  
} else {  
return “No such contact”;  
}  
}

So, I just learned the hard way that I need to loop through each index before returning anything.

---

<div class="post-metadata">

**Author:** ![enmcewan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/enmcewan/32/53476_2.png) [@enmcewan](https://forum.freecodecamp.org/u/enmcewan)\
**Post date:** [June 28, 2017, 1:50pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/7 "2017-06-28T13:50:53Z")

</div>

I did the exact same code as you, don’t feel so bad - or to put it another way, we are both idiots, lol.

---

<div class="post-metadata">

**Author:** ![AhmedMJ](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmedmj/32/60569_2.png) [@AhmedMJ](https://forum.freecodecamp.org/u/AhmedMJ)\
**Post date:** [June 30, 2017, 9:35am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/8 "2017-06-30T09:35:14Z")

</div>

I’m confused in the return line here :  
`return contacts[i][prop];`  
why it doesn’t work when i replace the brackets notation with the dot. linking, since both ways are valid.  
I mean like this  
`return contacts[i].prop;`

---

<div class="post-metadata">

**Author:** ![anon24349556](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@anon24349556](https://forum.freecodecamp.org/u/anon24349556)\
**Post date:** [June 30, 2017, 11:42am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/9 "2017-06-30T11:42:42Z")

</div>

Same problem here…

---

<div class="post-metadata">

**Author:** ![Nikhil0417](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nikhil0417/32/161817_2.png) [@Nikhil0417](https://forum.freecodecamp.org/u/Nikhil0417)\
**Post date:** [June 30, 2017, 6:11pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/10 "2017-06-30T18:11:56Z")

</div>

for(var i=0;i\<4;i++) {  
if(firstName === contacts[i].firstName && prop === contacts[i].prop)  
return contacts[i].prop;  
else if (firstName != contacts[i].firstName)  
return “No such contact”;  
else  
return “No such property”;  
}

_I could not figure out a mistake in my code. Is there something that I am missing?_

---

<div class="post-metadata">

**Author:** ![rodrigodiego](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rodrigodiego/32/59117_2.png) [@rodrigodiego](https://forum.freecodecamp.org/u/rodrigodiego)\
**Post date:** [July 1, 2017, 2:21pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/11 "2017-07-01T14:21:35Z")

</div>

I did this soluction. My code is not beaultiful, but it works.

function lookUpProfile(firstName, prop){  
// Only change code below this line

var checkFirstName = false;

for(var i=0;i\<contacts.length;i++){

```
if(contacts[i].firstName===firstName){
  checkFirstName = true;
  if(contacts[i].hasOwnProperty(prop)===false){
    return "No such property";
  } else {
    return contacts[i][prop];
  }
}

```

}

if(checkFirstName === false){  
return “No such contact”;  
}

---

<div class="post-metadata">

**Author:** ![brar3449](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/brar3449/32/58890_2.png) [@brar3449](https://forum.freecodecamp.org/u/brar3449)\
**Post date:** [July 2, 2017, 6:49pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/12 "2017-07-02T18:49:32Z")

</div>

This is what I used, but I was getting an error code telling me to write != as !== which does not work. I almost used the hasOwnProperty() method but I was having a hard time making that work and this worked. I feel like there might just be a bug with the editor telling me this is wrong, does anyone have any ideas why this would be happening.

for(i = 0; i \< contacts.length; i++) {  
if (contacts[i].firstName == firstName) {  
if(contacts[i][prop] != null) {  
return contacts[i][prop];  
} else return “No such property”;  
}  
}  
return “No such contact”;

---

<div class="post-metadata">

**Author:** ![catcattac](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catcattac/32/64397_2.png) [@catcattac](https://forum.freecodecamp.org/u/catcattac)\
**Post date:** [July 3, 2017, 12:45am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/13 "2017-07-03T00:45:15Z")

</div>

@AhmedMJ @anon24349556

> `contacts[i][prop]`  
> **vs**  
> `contacts[i].prop`

- We cannot called `contacts[i].prop` because` .prop` is not a property of `contacts`.

- When we access objects in bracket notation `contacts[i][prop]`, we can read and pass in values, such as the function arguments (example: “lastName”, “likes”, “numbers”, etc.)

> ```
> function lookUpProfile(firstName, prop) 
> 
> ```

> > `contacts[i][prop]` can pass in `prop` argument:  
> > `lookUpProfile("Kristian", "lastName");`

> > > ` var property = contacts[i]["lastName"];`  
> > > `property = "Vos";`

---

<div class="post-metadata">

**Author:** ![catcattac](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catcattac/32/64397_2.png) [@catcattac](https://forum.freecodecamp.org/u/catcattac)\
**Post date:** [July 3, 2017, 1:11am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/14 "2017-07-03T01:11:46Z")

</div>

```
function lookUpProfile(firstName, prop) {
                 for(var i=0;i<4;i++) {
                if(firstName === contacts[i].firstName && prop === contacts[i].prop)
                return contacts[i].prop;
                else if (firstName != contacts[i].firstName)
                return "No such contact";
                else
                return "No such property";
                }
            }

    lookUpProfile("Kristian", "lastName")

```

`for(var i=0;i<4;i++)`

- You should not use static number `4`, and instead call the array’s `.length`.

`if(firstName === contacts[i].firstName && prop === contacts[i].prop)`

- `contacts[i].prop` will not call the `prop` argument passed in, `"lastName"`.
- `contacts[i].prop` will call `contacts[i]` property named `"prop"`, which it does not have.
- You want `contact[i]` to call "`lastName"` property, which is accessible by calling `contact[i]["lastName"]`.
- That is why we use `contact[i][prop]`.

I would recommend checking for `firstName` logic seperately from `prop` - that will help. There are also syntactical errors (curly braces), use them 🙂

---

<div class="post-metadata">

**Author:** ![raulmcriado](https://avatars.discourse-cdn.com/v4/letter/r/ebca7d/32.png) [@raulmcriado](https://forum.freecodecamp.org/u/raulmcriado)\
**Post date:** [July 3, 2017, 12:41pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/15 "2017-07-03T12:41:00Z")

</div>

> [@JonFieldings](#):
>
> Man, I really feel like an idiot. I spent a couple of hours trying to figure this out, just to find that one of my return statements was out of place. Here my original code:
> 
> for (i = 0; i \< contacts.length; i++) {  
> if (firstName === contacts[i].firstName) {  
> if (contacts[i].hasOwnProperty(prop)) {  
> return contacts[i][prop];  
> } else {  
> return “No such property”;  
> }  
> } else {  
> return “No such contact”;  
> }  
> }
> 
> So, I just learned the hard way that I need to loop through each index before returning anything.

I did the same thing lol, but at least it was only 15 mins wasted \>\<

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [July 6, 2017, 12:29pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/17 "2017-07-06T12:29:36Z")

</div>

Hi guys, It took me the whole day and I think I am fully idiot for now because it still doesn’t work will. Here is the Code:

function lookUpProfile(first, prop){  
var result = “”;  
var contactTest = “firstName”;  
var propTest1 = “lastName”;  
var propTest2 = “number”;  
var propTest3 = “likes”;

for (var i=0; i\<= contacts.length; i++){  
var testName = contacts[i][contactTest];  
if (first === testName){  
switch (prop){  
case “lastName”:  
result = contacts[i][propTest1];  
break;  
case “number”:  
result = contacts[i][propTest2];  
break;  
case “likes” :  
result = contacts[i][propTest3];  
break;  
default:  
result = “No Such Property”;  
}  
}else {

result = “No Such Contact”;  
}  
}  
return result ;

}  
lookUpProfile(“Harry”, “number”);  
/\* main concept  
if firstname exsits \>\>\> check for property \>\>\> if it exsiting … return its value  
if Not … return no such property  
if firstname isn’t exsiting \>\>\> no such contact  
\*/

it gives me an error of (( Can’t read property ‘firstName’ of undefined )) !!  
what’s wrong now ?!

---

<div class="post-metadata">

**Author:** ![PatrickMoumiet](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/patrickmoumiet/32/76564_2.png) [@PatrickMoumiet](https://forum.freecodecamp.org/u/PatrickMoumiet)\
**Post date:** [July 6, 2017, 1:39pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/18 "2017-07-06T13:39:30Z")

</div>

It took me like 5 hours to understand that i had to put the return " No such contact" out of my loop  
^^.

for (var i = 0; i \< contacts.length; i++)

{

```
 if (firstName == contacts[i].firstName && contacts[i].hasOwnProperty(prop))  
 
      {
        
        return contacts[i][prop] ;
           
      }     
 
 if (!(contacts[i].hasOwnProperty(prop))) 
     
      { 
        
       return "No such property";      
      }     

```

}

return “No such contact”;

---

<div class="post-metadata">

**Author:** ![rikort](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rikort/32/161752_2.png) [@rikort](https://forum.freecodecamp.org/u/rikort)\
**Post date:** [July 7, 2017, 3:01am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/20 "2017-07-07T03:01:51Z")

</div>

Hi…So awesome to see everyone’s creativity.  
It took me two days so I am satisfied.  
Please share your thoughts on the code…

```
function lookUpProfile(firstName, prop){
    var value;
    for (var i = 0; i < contacts.length; i++){
      if((contacts[i].firstName === firstName) && 
         (contacts[i].hasOwnProperty(prop))){
        value = contacts[i][prop];
        break;
    }
    if (contacts[i].hasOwnProperty(firstName) === false){
      value = "No such contact";
    }
    if (contacts[i].hasOwnProperty(prop) === false) {
      value = "No such property";
    }
    }
    return value;
    }
```

---

<div class="post-metadata">

**Author:** ![pensivepanda](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pensivepanda/32/60254_2.png) [@pensivepanda](https://forum.freecodecamp.org/u/pensivepanda)\
**Post date:** [July 7, 2017, 10:01pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/21 "2017-07-07T22:01:24Z")

</div>

First time poster! One question, going off the basic code solution is their an alternative way to check if the prop argument matches the property of the object besides using "

if (contacts[x].hasOwnProperty(prop)) {  
return contacts[x][prop];  
} else {  
return “No such property”;  
" .

I can’t wrap my head around why this other alternative method would not work.  
if(contacts[i][prop]===prop){  
return contacts[i][prop];  
}  
else{  
return “No such property”;  
}

---

<div class="post-metadata">

**Author:** ![Kagerjay](https://avatars.discourse-cdn.com/v4/letter/k/4af34b/32.png) [@Kagerjay](https://forum.freecodecamp.org/u/Kagerjay)\
**Post date:** [July 8, 2017, 3:03pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/22 "2017-07-08T15:03:59Z")

</div>

> [@catcattac](#):
>
> You should not use static number 4, and instead call the array’s .length.

I just realized that this example is following

`contacts` is an **array** of objects. So `.length` is only for the properties of an **array**

`likes` is also an **array** as well

`.hasOwnProperty` is a property of an **object.**

Also for original solution

```javascript
return contacts[x][prop];

```

am not sure why this doesn’t work

```javascript
return contacts[x].prop;

```

![](https://global.discourse-cdn.com/freecodecamp/original/3X/1/c/1c5917f15936188138db9befc2971442b246e35f.png)

Using

```javascript
return contacts[x].likes;

```

returns this

![](https://global.discourse-cdn.com/freecodecamp/original/3X/e/d/ed7be59375ac475838c0227b955c2d40426a93fe.png)

I think the program cannot tell whether its a variable that its being passed or an actual property of the object using dot notation

stackoverflow says this is correct

> <https://stackoverflow.com/questions/7102704/how-to-use-variables-in-dot-notation-like-square-bracket-notation>

To understand the context of this, you can convert `firstName` into bracket notation which requires a quotation marks (from original solution)

following code works

```javascript
function lookUpProfile(firstName, prop){
// Only change code below this line
  for (var x =0; x<contacts.length; x++){
    if (contacts[x]["firstName"] === firstName){
      if (contacts[x].hasOwnProperty(prop)){
        return contacts[x][prop];
      } else {
      return "No such property";
      }
    }
    
  }
return "No such contact";
// Only change code above this line
}

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

```

In summary, you should use bracket notation

- when property name is contained in a variable, in this case **prop** , but not **firstName** (the variable being passed to function, the property of the object “firstName” is ok though) → _this is confusing how the program named it, these should’ve been different names._

- Property name contains characters not permitted in identifiers (Space, `-` ↑ unicode symbols, etc)

else use dot notation

see this stackoverflow post:

> <https://stackoverflow.com/questions/17189642/difference-between-using-bracket-and-dot-notation>

---

<div class="post-metadata">

**Author:** ![Kagerjay](https://avatars.discourse-cdn.com/v4/letter/k/4af34b/32.png) [@Kagerjay](https://forum.freecodecamp.org/u/Kagerjay)\
**Post date:** [July 8, 2017, 3:51pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/23 "2017-07-08T15:51:25Z")

</div>

Alternative solution without using `hasOwnProperty` but rather a `for ....in` loop

```javascript
function lookUpProfile(firstName, prop){
// Only change code below this line
  for (var x =0; x<contacts.length; x++){
    if (contacts[x].firstName === firstName){
        for (var keys in contacts[x]){
          if (keys === prop){
            return contacts[x][prop];
          } 
        } 
        return "No such property";
      }
    }  
return "No such contact";
}

```

---

<div class="post-metadata">

**Author:** ![thelynguist](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thelynguist](https://forum.freecodecamp.org/u/thelynguist)\
**Post date:** [July 9, 2017, 7:07pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259/24 "2017-07-09T19:07:54Z")

</div>

Question:

Why does “return contacts[i][prop];” return the first name?

Technically, [i] represents an array of names that each have a certain value, right? So why does the [i] only return the first name?

[Next page](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259.md?page=2)
