Just some help needed [spoiler] JS NEW

var contacts=[
  {
  
  "firstName":"Akira",
  "lastName":"Laine",
  "number":"0000000001",
  "like":["pizza", "Coding", "Brownie Points"]
    },
  {
    "firstName":"Harry",
  "lastName":"Potter",
  "number":"0000000002",
  "like":["Hogwarts", "Magic", "Hagrid"]
    },
      {
          "firstName":"Sherlock",
  "lastName":"Homes",
  "number":"0000000003",
  "like":["Intruiging Cases", "Violin"]
        },
          {
              "firstName":"Krsitan",
  "lastName":"Vos",
  "number":"0000000004",
  "like":["JavaScript", "Gamin", "Foxes"]
            }
]

function lookup (name, prop){
  for (var i=0; i<contacts.length;i++){
    if (contacts[i].firstName===name){
      return contacts[i][prop]||"No such property"
      }
    }
  return "No such contact"
  }

var data=lookup("Akira", "like")

console.log(data)

So, what I do not really know, is what is going on within the function. I understand that the function is checking: the "name", and the property (in this case, I used "likes"). Then there is a for statement that is going to increment i:
for (var i=0; i<contacts.length;i++), and increase its index within the array. However, I do not understand what is going on in the statements:
if (contacts[i].firstName===name) and
return contacts[i][prop]||"No such property".

An explanation would be greatly appreciated. :slight_smile:

Hello!

if (contacts[i].firstName===name) is checking each contact in the loop. If the contact’s first name matches the name argument passed in the function, the return statement in the if loop is processed.

return contacts[i][prop]||"No such property" This is what runs when the if condition is true. The return statement will return the value for the prop argument passed in the function. In your example, the prop argument is "like", so the function returns the value for "like". The || is the “or” operator, and acts as a fallback. If the contacts[i][prop] is undefined, the function returns "No such property" instead of returning undefined.

Try changing lookup("Akira", "like") to lookup("Akira", "hate") to see this in action.

1 Like

So, let me get this straight. The if statement is checking the var contact, and seeing if the first name matches the one which was entered. Then, the
return contacts[i][prop]||"No such property" will check if the second property exists and if so, return it. The return contacts[i][prop] part will check the index, and see if it has what i is and what the property is . Otherwise, it will say, No such property. And towards the end of the function, if neither of the contacts exists, then it will return no such contact exists.

Yes, the if statement confirms that the object in the contact array has a firstName value matching name.

The return contacts[i][prop] itself doesn’t actually check if the object has the property prop. If it doesn’t, the function would return undefined.

The || operator is what tells the code to return "No such property" if the value of contacts[i][prop] is “falsy”. undefined is considered a “falsy” value, so the || operator takes effect.

You can see this in action here, where I’ve called your function with “Akira”, “number”. But because I’ve changed the “number” value to 0, the function sees it as a “falsy” value and returns “No such property”. (This would not work if I changed the value to "0" as a string, though).

“Akira” still has the “number” property, which shows that the function doesn’t check if the property exists. It just checks if the value of the property is falsy.


This screen shows that Akira still has the “number” property.