Profile Lookup hints

Tell us what’s happening:

I believe my if else statements are correct to meet the conditions of the challenge but the loop won’t work. And I don’t think I have to use nested loops because if I do this:
for (var i=0; i < contacts.length; i++) { console.log(contacts[i].firstName); }

This will print the first name of each object, which I am trying to use as my condition.

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].hasOwnProperty(name)) {
   return "No such contact";
    } else {
   return "No such property";
    }
  }
// Only change code above this line
};

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) 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

Ummm I see… Yes that’s true, so I must choose another path to solve this challenge.

If the name doesn’t exist in any of the objects I have to return “No such contact”, hasOwnProperty evaluates to true or false whether the obj has the specified property or not, but I was missing the comparison, so if it’s false then return “No such contact”.

arr.hasOwnProperty === false {...}

I am not sure if that’s what you meant but I think it fixes my mistake.

When I call the function with the arguments.

lookUpProfile("John", "likes")

This should return “No such contact” because John doesn’t exist in any object.

Well this was a great hint, I’m not sure if I came up with the best solution but at the end I managed to solve it after some struggle and taking a break:

function lookUpProfile(name, prop){

for(var i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop) === false) {
  return "No such property";
 } else if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
  return contacts[i][prop];
  } 
 } return "No such contact"

};

However there’s something that it’s not very clear to me, I know my original code has a few mistakes but why the loop will not check the first if statement, instead it will return “No such property”, so it goes straight to the else block w/o checking the previous conditions?

Ummmm, I think I get it now, basically it’s the order on which I stated my conditions. But I’m still trying to understand why my first code iterates only once and exits the function.

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].hasOwnProperty(name)) {
   return "No such contact";
    } else {
   return "No such property";
    }
  }

If the following conditions are not met
if(contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) { return contacts[i][prop];
Then the block of code within this statement should never be executed and continue with the loop.

edit: Ohhhh never mind, it’s because the else statement at the end of the loop… That’s why it only iterates once if none of the previous conditions are met. My other code works because I instinctively put “No such contact” condition out of the loop. Following your advice I thought the only way to know if there’s no contact with such name was after finishing iterating the loop.

Wow it felt like such a guess work but it’s so obvious now. On the other hand I had no idea I could use if else statements like that, I originally thought I’d need an if statement condition to initiate the loop and then some if else nested statements within the loop. I guess that could be another approach.

Well, it’s clear to me now, thanks a lot!