Basic JavaScript: Profile Lookup (Challenge)

Tell us what’s happening:
So ive been struggling with this code for about 40 min and i just realized that there isnt anything wrong with it the problem is that at some point in time during the iteration name actually doesnt match the firstName of an element. how can i fix this?

Instruction: 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"

Your code so far

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

// Only change code above this line

// Change these values to test your function
lookUpProfile("Bob", "number");`
**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36</code>.

**Link to the challenge:**

Hint: As soon as your code executes a return statement, the function exits immediately. You need to rethink your algorithm and figure out when and what checks need to be made to solve the challenge. Your code only ever looks at the first contact object before returning a value ( contacts[i][prop], "No such contact", or "No such property".

1 Like