Basic JavaScript - Profile Lookup [HELP]

I am super lost:

Here is the code I got for now which returns 2 correct answers:

if ("firstName" === name) {
    return value;
  } else if ("firstName" !== name) {
    return "No such contact";
  }

link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

Ok, you have a contacts array with a number of objects inside it.

You need to check each of the objects within the array, to determine if any of them have a firstName property which matches name. To do this you will need some kind of iterative loop.

Inside the loop, you will check each object. If an object has a matching name then you need to check if it also contains a property which matches prop.

If both of these conditions are true, you return the value of that property.

If only the first is true, you return “No such property”.

If you reach the end of the loop and find no matching name, you return “No such contact”.

Breaking it down into simple logical steps like this is the first step.

Now you need to implement each step using an appropriate loop and accessing object properties accordingly, using dot or bracket notation.

1 Like

the hint says to use the for loop, but I don’t know where to start with or how to use it in this challenge.

A basic for loop would work, yes.
So, say I wanted to print all values in an array to the console:

let array = [1, 2, 3, 4, 5];
for (let i=0; i<array.length; i++) {
  console.log(array[i]);
}

That for loop iterates every item within array.

Implement a loop like this but for the contacts array.
Inside the loop you can then write conditional logic which checks each of the items (objects) within contacts.

1 Like

so it is going to be like this:

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

but idk how to check for the name… I tried the code that I put in the post but it didn’t work.

sorry I am just super confused…

To access an object’s properties you should use dot or bracket notation. The article linked above explains which you should use, in which cases.

EXAMPLE:

let user = {
  name: "igorgetmeabrain",
  age: 120
};

console.log(user.name); // igorgetmeabrain
console.log(user["name"]); // igorgetmeabrain

let property = "age";
console.log(user.property); // undefined
console.log(user[property]); // 120

This firstly represents two different ways (dot and bracket) of accessing the name property explicitly, within the object user.

Then it shows how you can access an object property when the name of the property (e.g. age) is stored within a variable. Note that you cannot use dot notation to access an object property dynamically (i.e. via a variable). This is why the above code will return undefined if you do. You must use bracket notation (without quotation marks).

In short, in your code, you can use dot notation to access the firstName property in contacts. However, you must use bracket notation to access the object property which is passed to the variable prop.

I hope that makes some kind of sense? Perhaps the article I linked will explain it more fully.

1 Like

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