Basic JavaScript - Profile Lookup

Tell us what’s happening:
Describe your issue in detail here.
I used the answer provided by freeCodeCamp but I don’t know how they got the answer. There are comments provided in each and every line of code with a question, if possible and if you have the time, can you answer each and every one of them? Thank you.

  **Your code so far**
// 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 x = 0; x < contacts.length; x++) { /* What's the point of the variable x and why should it count the length of contacts? */
  if (contacts[x].firstName === name) { //Why is x being used here?
    if (contacts[x].hasOwnProperty(prop)) { //Why is x being used here?
      return contacts[x][prop];
    } else {
      return "No such property"; /* Why is this first when it relates to the second if statement? */
    }
  }
}
return "No such contact"; 
} // Why is this return on the bottom when it is related to the first if statement?
// Only change code above this line // Thanks again!

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/103.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

Maybe you should go back to, for example, the loops exercises so you can understand what for loops do and how it works. You can also look for info online, like on MDN: for - JavaScript | MDN

Okay I will do that!

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