What's actually wrong in my code?

Tell us what’s happening:
Describe your issue in detail here.
I am stuck on this chalenge for long. FirstI tried to solve it by myself but when I did not succeed I went for hint. I still can’t get it what’s wrong in my code.

   **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 i = 0; i < contacts.length; i++) {
 if (contacts[i].firstname === name){
   if (prop in contacts[i]) {
      return contacts[i][prop];
   }
else{
  return "No such contact";
}
 }
}
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/98.0.4758.102 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

Lets edit this for readability so we can see the logic:

function lookUpProfile(name, prop) {
 // Only change code below this line
  for (let i = 0; i < contacts.length; i++) {
    if (contacts[i].firstname === name){
     if (prop in contacts[i]) {
       return contacts[i][prop];
     } else {
       // WHEN DOES THIS LINE RUN?
       return "No such contact";
     } // end of prop if
   } // end of name if
 } // end of for loop
 return "No such property";
 // Only change code above this line
}

Now, looking at this, when will you reach the messages "No such contact" and "No such property"?

Also, note the difference between firstname and firstName.

1 Like

Mod Edit: SOLUTION REDACTED

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

when writing contacts[i].prop u are accessing an attribute called prop and not the prop value, make sure to get the value by doing this contacts[i][String(prop)]

I don’t see where the OP wrote contacts[i].prop

Why the cast to a string? prop holds a string already.

Oh yeah now I got it
Thanks for helping,Mate :smiling_face_with_three_hearts:

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