Guibuck90 - profile lookup

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": "You",
        "number": "unknown",
        "likes": ["JavaScript", "Gaming", "Foxes"]
    }
];

function lookUpProfile (name) {
      for (var i = 0; i <contacts.length; i ++) {
        if (contacts [i] .firstName === name) {
          return contacts [i] [name]; // does not return the complete obj
          }
        }
        return "Not found";
      }
  console.log (lookUpProfile ("kristian"));

If it is not too much trouble, I would like to get rid of doubts, why does this code not work? here the for loop would have to iterate and go through the array but it doesn’t

Hello and welcome to the freeCodeCamp community~!


In the future, please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Ask for Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Now, on to your question.

What do you believe contacts[i][name] is accessing? Do you know what the value of contacts[i] might be?

I think that the values ​​that “contacts [i] .firstName” would have to show are all the names of the objects that the array “contacts” has, but when sending the value “Kristian” as a parameter, I get Not Found as a result, and Kristian does is an obj name

contacts[i] would be a single object within the contacts array.

contacts[i][name] would be accessing the property of that object that is called the value of name. In the first example, the value of name is Kristian, so you are trying to return the value of contacts[i].Kristian. Does the object have a Kristian property?

I understand what you mean, but this is just a more basic example that I was doing without the other property, it was to understand why this happens when iterating the for together with the if, what catches my attention is that the if does not return a result correct, the point is that I would have to iterate the array of objects, when it reaches the if condition it derives it by two options, if the object with the firstName property is equal to the name that I send by parameter then it returns the name again, and if it is not the same, it returns not found, but when entering the name it always returns not found, and maybe I’m making a mistake in something but I don’t quite understand why it returns not found if the name of the object is correct and I think the syntax too

Thank you for taking the time to answer my question, thank you very much really

function lookUpProfile(name) {
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === name) {
      return contacts[i][name]; // does not return the complete obj
    }
  }
  return "Not found";
}

Okay, let’s take another look at your code (which I’ve run through a formatter to get rid of the extra spacing).

for (var i = 0; i < contacts.length; i++) { - This line tells the code to loop, or iterate, through the contacts array.

if (contacts[i].firstName === name) { - This line tells the code to check if the object (contacts[i]) has a value in the property firstName that matches the value of name - the argument we passed in to the function.

return contacts[i][name]; - IF the previous check is true, this line says "Give back the value of contacts[i][name].

return "Not found"; - If that previous check is never true, once the loop has gone through the entire array, this line will say “Tell me you didn’t find a contact”.


So, now that we’ve taken a look at the logical flow of your code, let’s take a look at the live function call: lookUpProfile("Kristian"). Here, the value of name is "Kristian" - this is important as we will need to remember this.

The function begins with the loop. It checks contacts[0] first - what’s the value of the firstName property? "Akira". That does not match the value of name we gave, so it loops again. It checks contacts[1] - the firstName property here is “Harry”, which does not match. On to contacts[2] - the firstName property is “Sherlock”, which does not match. Finally contacts[3] - the firstName property is “Kristian”… we have a match! Great! Let’s return contacts[i][name].

Our return statement evaluates to contacts[3]["Kristian"]. We know contacts[3] exists, but what is the value of the Kristian property? Wait… there isn’t one! The contact object does not have a Kristian property… So the value is undefined, which is what your function will return.

The return statement will need to be adjusted to return the correct value.


Now that we’ve looked at the logical flow of your function, we have another thing to look at. When you call lookUpProfile("kristian") you are getting "Not found" as your return value. Why? Because "kristian" and "Kristian" are not the same thing - in JavaScript, the casing of strings is important.


As one final note, the function you have written has been malformed from the challenge seed. The function should take two parameters, a name and a prop. You’ve removed the prop parameter, and will not be able to pass the challenge until that is corrected.

1 Like
function lookUpProfile(name, prop) {
    for (var i = 0; i <contacts.length; i ++) {
      if (contacts [i].firstName === name) {
        return contacts[i][prop]; // does not return the complete obj
        }
      }
      return "Not found";
    }

console.log (lookUpProfile("Kristian", "firstName"));

//-----------------------------------------------------//

I put the property of firstName as it told me and for the first function it gives me the result, but for this other function below where I use an if () {.... return} else {... return} it gives me the result Not found, why is this due?

function lookUpProfile(name, prop) {
      for (var i = 0; i <contacts.length; i ++) {
        if (contacts [i].firstName === name) {
          return contacts[i][prop]; // does not return the complete obj
        }else{
          return "Not found";
        }
      }
    }
console.log (lookUpProfile("Kristian", "firstName"));

contacts[i][prop] should not be returning a complete object. You want the function to return the value of the property, so this line is correct.

Your first function structure is closer than your second. That if/else you added will terminate the loop on the first iteration. Stick with your first example:

function lookUpProfile(name, prop) {
    for (var i = 0; i <contacts.length; i ++) {
      if (contacts [i].firstName === name) {
        return contacts[i][prop]; // does not return the complete obj
        }
      }
      return "Not found";
    }

This one is passing tests! You’ve made progress!

Now you need to handle the “No such contact” and “No such property” checks. :slight_smile:

1 Like

Why with the second function the cycle will end in the first iteration?

function lookUpProfile(name, prop) {
      for (var i = 0; i <contacts.length; i ++) {
        if (contacts [i].firstName === name) {
          return contacts[i][prop]; // does not return the complete obj
        }else{
          return "Not found";
        }
      }
    }

Let’s look at the structure again. We are going to call lookUpProfile("Kristian", "firstName"). The function begins with an iteration through the contacts array.
It starts with contacts[0].

if (contacts [i].firstName === name) - contacts[0] value of firstName is “Akira”, which does not equal “Kristian”. So the if statement is false, and does not run.

 }else{
          return "Not found";
        }

Because the if check was false, the else statement runs. The else statement says return "Not found";. Remember that return ends a function, so the function gives back the value “Not found” and is done running. Our loop has now failed to check any other contact after the first.

1 Like

Now I understand Nicholas, thank you very much for taking the time to explain to me

1 Like

I am glad I was able to help! Happy coding! :purple_heart: