Profile Lookup/Query on valid property check

Tell us what’s happening:

I can’t seem to get a return for objects which have no valid property. I have left the [prop] check within the loop but I am stumped as to where I’ve gone wrong.

A little guidance in the right direction or perhaps proofreading would be helpful.

Thanks for anything,
Nick

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(name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)){
       return contacts[i][prop];
} else {
       return "No such property";
}
}





// Only change code above this line
} return "No such contact";

// Change these values to test your function
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/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

function lookUpProfile(name, prop){
  for(var i = 0; i &lt; contacts.length; i++) {
     if(name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)){
       return contacts[i][prop];
     } else {
       return "No such property"; // if the first object in the loop is not a match, this is stopping the function execution
     }
  }

} return "No such contact"; // right now this line is outside of the entire function

I put a couple of comments next to some issues. I think the return statement being outside of the function is likely an error when you were pasting, but the else clause inside of your loop is the problem.

What is happening now is your function exits after the first iteration of the loop, whether or not that object is the one you’re looking for.

You’re saying ‘If this current object has the first name I am looking for AND it has the property I am looking for’. If it’s true, it returns the value. If not, it returns anyway. The function is done now no matter what.

You need to break things down further with the loop.

Is this object the one I am looking for?

  • If so, does this object have the property I am looking for?
    • if so, return the value of the property
    • Otherwise return that the property does not exist
  • If the object is not the one I am looking for, continue the loop.

I have kept all potential conditions within the loop and I pass all tests for objects within the contacts array; however, for those which should return “No such contacts/property” I fail.

When I move curly brackets so that “No such contacts/property” are outside the loop (but not the function), then I pass “No such contacts”, but fail tests concerning true conditions within the contacts array.

Regardless of what avenue I take, I do not pass “No such property”.

Where have I erred?


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

}
}






// Only change code above this line


// Change these values to test your function
lookUpProfile("Akira", "likes");
 if(prop !== [name]) 

You have put the name variable into a new array and then you are trying to compare the prop variable to the new array. I am not sure what you are trying to do here.

if(name !== contact[i][prop])

This is never going to be true and I am not sure again what you’re attempting. So in the example above, name is ‘Akira’ and prop is ‘likes’. Let’s say i is 0, the first contact, which again is Akira. contact[0][likes]. ‘Akira’ is not equal to the array of likes. No string is ever going to be equal to an array.

Also:

   if (prop !== [name]) {
          return "No such property";
          if (name !== contact[i][prop]) {
            return "No such contact";
          }
        }

None of that code is ever going to run because right before it, you’re returning and the function stops executing.

Your original function was very, very close. You just needed to move some logic around.

Try thinking about this as simply as possible:

  • Check to see if the current contact’s firstName matches the name passed into the function (you’re doing this and it’s in the right spot)
  • If so, then check to see if that contact contains the property you’re looking for. (You’re doing this, it’s in the right spot, but what you’re doing inside and after this is not correct).
    • Path 1: Yay! The property is found! Return the value;
    • Path 2: (else) Boo. The property is not found. Return “No such property”

That is all you need to do inside of the loop, because if none of the contacts match the name given, then we know the contact does not exist, and we can return “No such contact” at the end of the function.

Everything works when I follow your advice EXCEPT when I add an else{return “No such property”} after returning my value from the loop I fail all other tests except that for returning “No such property”. I’m not quite sure where to go from here because everything appears to me to be solid…but this is not the case of course. Where am I going wrong?


//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(name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
    }   
}
return "No such contact";
}

// Only change code above this line


// Change these values to test your function
lookUpProfile("Akira", "likes");

I believe it has something to do with line 33, where I use

contacts[i].hasOwnProperty(prop)

I think I should alter prop, but making it an array didn’t do anything, so I’m not so sure about that solution.
The other idea I’ve had is altering contacts[i] in someway, but doing this would mess-up one of the explicit tasks of discerning if the contact for which I’ve determined has name === firstName is coordinated with my prop check.

I don’t know where to tweak things. Rather, I can see where but I don’t know how that won’t adversely affect the code I’ve already written.

You should be adding the else clause for “No such property” inside if the loop, not after the loop is finished.

Inside of the loop, the first thing you need to do is check if the name value passed to the function matches that contact’s firstName property. You’re doing this. But then you keep making a similar mistake and adding the next condition in the same line:

&& contacts[i].hasOwnProperty(prop)

does not belong there because there are TWO scenarios you need to address when the name is a match:

  1. The name matches and the contact has that property
  2. The name matches, and the property does not exist

How can you break up your if statement to address these issues?

Remember you can nest if statements. You can check to see if the name matches, and then inside of that statement, check to see if the contact has that property or not.

Don’t quite think I’m getting it still. I’ve nested if statements, but now I can’t get the value for the if statements to return the contacts data I was getting before. Along with this I’m having issue with “No such property”. I’m including it in the loop as an else statement to “The name matches and the contact has that property”. I thought that would be enough, but I’m still missing something.


//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(name === contacts[i].firstName){
        if(name === contacts[i].hasOwnProperty(prop)) {
            return contacts[i][prop];
        } else {
                return "No such property";
        }
        }
            }

        }
    
        
return "No such contact";



// Only change code above this line


// Change these values to test your function
console.log(lookUpProfile("Akira", "likes"));

You need to look carefully at what you are comparing:

 if(name === contacts[i].hasOwnProperty(prop)) 
  1. name is always going to be the first name of the contact you’re looking for. ALWAYS. Never will you want to compare name to anything except for contacts[i].firstName

  2. contacts[i].hasOwnProperty(prop) returns a boolean, so right now you’re saying “if name is true or false”.

  3. Your loop nesting is correct right now, but you need to change that if statement I quoted. you don’t need to compare the prop to anything. All you’re trying to do is see if that contact has that property. So how would you change if(name === contacts[i].hasOwnProperty(prop)) to just evaluate to whether or not the property exists? That’s all you need to do in that block.

  4. Right now, return "No such contact" is completely outside of your function.