Profile lookup - don't understand why for and no access used to the array

I wrote a code, which does not pass the challenge. I read other post and watch the video. There are some points I don’t understand:

  1. why do I use a loop for the lookup and how do I recognise it when they ask for it in the task
  2. why don’t they use the array access when they call the variable “contacts”
  3. how can I re-edit my code?
  4. how do I know that I have to use contacts.length if this is the array with object contacts?

The questions can be silly, but I spent hours and don’t understand :frowning:
Thank you for the explanation.

  **Your code so far**

// 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

if (name === contacts[0].firstName && contacts[0].hasOwnProperty(prop)){
  return contacts[0][prop];
} else if (name !== contacts[0]) {
  return "No such contact";
} else (prop !== contacts[0][name].hasOwnProperty(prop)); {
  return "No such property";
}

// Only change code above this line
}

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/95.0.4638.54 Safari/537.36 Edg/95.0.1020.30

Challenge: Profile Lookup

Link to the challenge:

The biggest hint that you want to use a loop is the fact that we have an array of similar items. In this case, we have an array of contacts.

In this challenge, we need to ‘go through’ our ‘list’ of contacts and look for the one with the matching name. Well, ‘list’ is really an array, and to ‘go through’ an array we need to loop through the contents.

I’m not exactly sure what you mean by ‘array access’? The syntax contacts[0] means 'get the item from the contacts array at index 0'. Or more commonly, the syntax contacts[i] means 'get the item from the contacts array at index i'.

We use array.length in problems like this because the length of the array tells us how many items we need to ‘go through’.

1 Like

By “array access” is this what you wrote exactly.
Thank you! that helped to get to the below code.

So I’ve re-written the code, which I’d like to share. I will be appreciated for checking my understanding.

function lookUpProfile(name, prop) {

// Only change code below this line

for (var i = 0; i < contacts.length; i++) { // I initiate loop for, then my statement says check everything included in the array, after go to the next index/item

if (name === contacts[i].firstName) { // the condition if will check name appears in contacts (strict equal), if it is true it will check 2nd condition, but if it is false it will return “no such contact”, won’t it? the 2nd condition will not be checked.

if (contacts[i].hasOwnProperty(prop)) { //2nd condition checks if the array has the property parameter, if it has then return if not it will jump to else and display the statement from there

return contacts[i][prop];

} else {

return "No such property";

}

}

}

return “No such contact”; .// this return will pop up if non-conditions are true

// Only change code above this line

}

lookUpProfile(“Akira”, “likes”);