Profile lookup check

Tell us what’s happening:
Describe your issue in detail here.
can someone help me see what mistake I am making in my code? When I left return “No such property” out of the for loop the code did not run.

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

Challenge: Profile Lookup

Link to the challenge:

function lookUpProfile(name, prop) {
  // Only change code below this line
  for (var i = 0; i < contacts.length; i++)  {
    if (contacts[i][firstName] === name && contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop] ;
    } else if (contacts[i][firstName] !== name) {
      /* **INSIDE** OF THIS LOOP, CAN YOU SAY NO CONTACTS MATCH? */
      return "No such contact";
    } else if (contacts[i][firstName] === name && contacts[i].hasOwnProperty(prop) === false ) {
      return "No such property";
    }
  }
 
// Only change code above this line
}

When can you say that "No such contact" exists? A return statement immediately stops the function, so right now your function immediately stops if it finds a contact with a name that does not match.

1 Like

should I write separate loops for each condition?

There are 3 possible outcomes:

  1. You find a matching first name and property
  2. You find a matching first name but no property
  3. You find no matching first name

Why would you need three separate loops to handle that? Three separate loops would not fix the fact that you cannot say inside of the loop over all contacts that there is "No such contact".


Again, as soon as your function hits a return statement, your function (including the loop) immediately stops doing anything. This means that your function stops the very first time it finds a contact that does not match. Is this what the challenge is asking for?

1 Like
for (var i = 0; i < contacts.length; i++)  {

  if (contacts[i].firstName === name) {

      if ( contacts[i].hasOwnProperty(prop)) {

          return contacts[i][prop];

      }

      else {

  return "No such property";

  }

    

  }

}

  

      return "No such contact";

so here is my final code and it worked, however, when I used bracket notation instead of dot notation to access firstName, it didn’t work. Why would that be?

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 (’).

You can only use dot notation with the exact name of the property you want.

myObject.thisMustBeTheExactName

You can also do this with bracket notation and a string literal.

myObject["thisMustBeTheExactName"]

If you have a variable that holds the property name in a string, then you can only use bracket notation.

myObject[thisIsAVariableWithTheStringName]
1 Like

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