Profile Lookup (problem)

I don’t know why we are using a for loop. Can someone explain for me please?


// 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(name, prop) {
// Only change code below this line
for(var i=0; i<contacts.length; i++){

}
// Only change code above this line
}

console.log(lookUpProfile("Akira", "likes"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

Well, you have an array of profiles and you need to check each one. How are you going to do that other than looping? You could also use a for...of loop - that might even be a little “better” - if you’re comfortable with it. You could use while or do while but I think those are not as good. You could also do something like an array prototype method - but those are still looping, it’s just that the looping is hidden from you.

But I think a standard for loop is the most basic way to solve this, especially at the level at which I am assuming you are.

Does that make sense? We are using a for loop because we need to check each profile in the array, one by one. A standard/basic way to do that is to loop though the indexes and use them to index into the array as we check them.

For example:

  for (var i = 0; i < contacts.length; i++) {
    console.log('profile # =', i, ' --- first name =', contacts[i].firstName)
  }

Is it write if I just say if (contacts.firstName === name)
or
if (contacts[i].firstName) ?

Those check two different things. Either could work in various situations.

In the first example:

if (contacts.firstName === name)

You are asking if contacts has a property that matches exactly name. But in this case contacts is an array, so perhaps you meant:

if (contacts[i].firstName === name)

That is asking if that array has an element at the index i that matches name.

The second one:

if (contacts[i].firstName) 

is asking if that array has an element at index i and that that element has a property called firstName that is defined with a truthy value. In other words, it will fail if that value is not define or has one of the 8 (OK, 9) falsy values in JS. Since we’re talking about strings here, we’re asking if it is defined and is not an empty string. That is a perfectly valid thing to check in certain situations. i don’t know if it needs to be checked here.

Does that make sense?

Yap, thanks very much for the clarification

Hi, I am stuck with the rest parameter. I’ve tried to search but I haven’t yet understand the logic behind it.

Hi there,

For a new challenge and new question, you should make a new post :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.