Javascript basic Help , please check my code below

Basic JavaScript: Profile Lookup

We have an array of objects representing different people in our contacts lists.

A lookUpProfile function that takes name and a property ( prop ) as arguments has been pre-written for you.

The function should check if name is an actual contact’s firstName and the given property ( prop ) is a property of that contact.

If both are true, then return the “value” of that property.

If name does not correspond to any contacts then return "No such contact" .

If prop does not correspond to any valid properties of a contact found to match name then return "No such property" .

// 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)
{
for (var i = 0; i < contacts.length; i = i + 1 )
{ 
{ if (contacts[i].hasOwnProperty(prop) && contacts[i].firstName == name) {return contacts[i][prop];}
 else if (contacts[i].firstName !== name ) {return "No such contact"}
 else if (!contacts[i].hasOwnProperty(prop) && contacts[i].firstName == name ) {
     return "No such property" }
}
}}

lookUpProfile("Akira", "likes");

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hi and welcome to the forum!

function lookUpProfile(name, prop) {
  for (var i = 0; i < contacts.length; i = i + 1) { 
    { // Misplaced brace here
    if (contacts[i].hasOwnProperty(prop) && contacts[i].firstName == name) {
      return contacts[i][prop];
    } else if (contacts[i].firstName !== name) 
      return "No such contact" // Have you checked every contact at this point?
    } // Misplaced brace here
    else if (!contacts[i].hasOwnProperty(prop) && contacts[i].firstName == name ) {
      return "No such property"
      } // Extra brace here?
    }
  }
}

I tried to format this to something that others can read. I think you have some serious oganizational issues. It is difficult for me to understand what your code is doing due to the misplaced {}s and whitespace, and i think it is half of the issues you are seeing.

You should use your braces and whitespace to help you structure your logic.

for (initialize; condition; iterate) {
  // Loop body goes here
}

and

if (condition 1) {
  // Code here
} else if (condition 2) {
  // Code here
} else {
  // Code here
}

Thank you.
Could you please explain why you wrote “have you checked every contact at this point”? and why that is an issue.

I appreciate the help

You can only say that the contact is not in the list after you have checked every contact in the list.

1 Like

Make sure nothing is stopping your code from checking every contact in the list.

1 Like

Thank you!

function lookUpProfile(name, prop)
{
for (var i = 0; i < contacts.length; i = i + 1 ) { 
    if (contacts[i].hasOwnProperty(prop) && contacts[i].firstName == name) {
     return contacts[i][prop];
     }  
   else if (!contacts[i].hasOwnProperty(prop) && contacts[i].firstName == name ) {
     return "No such property" }
}
return "No such contact";
} 
lookUpProfile("Akira", "likes");
1 Like

Good work getting it to pass!

I’d still fix the code formatting in the future; the formatting helps with clarity, readability, and debugability. All of those are key in coding jobs, especially when working on a team with others!

1 Like