Can someone help me to solve profile lookup?

Can someone help me to solve profile lookup?
0

#1
    {
        "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"]
    }
];

var ans=0;
function lookUpProfile(firstName, prop){
// Only change code below this line
  for(var i=0;i<contacts.length;i++){
    if(firstName==contacts[i].firstName&&contacts.hasOwnProperty(prop)){
      for(j=contacts[i].prop;j<contacts[i].prop.length;j++){
        return contacts[i][prop];
      }
    }
    else if(firstName!=contacts[i].firstName){
      return "No such contact";
    }
    else if(contacts[i].hasOwnProperty(prop)===false)
       return "No such property";
    }
   
  
  
    
  

// Only change code above this line
}

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

#2

I think you are trying to do too many things at the same time.
The general algorithm would be:
Loop through contacts until you find one with a matching first name.
If no contact is found it does not exist
otherwise check the found contact has the property, if it does then return it

Also use ‘===’ instead of ‘==’, its a more robust way of equality checking


#3

Morning,

The profile lookup challenge does seem confusing at first. But it’s a lot easier once you go through its steps one by one in detail with a bit of explanation. I am going to go through the whole challenge below. (It’s going to be long, but hopefully, you or someone else might find it useful.)

Profile Lookup Challenge


Starting off with stuff you might already know:
You need to create a function that checks:

if (firstName is an actual first name in one of the contacts objects) AND
if (The property prop is a property that exists in the same contacts object.)
return the value of that key/property prop if both conditions are true.

You also need to return:

return "No such property"
if (The property prop does not exist in contacts object which contains firstName.)

return "No such contact"
if (firstName does not exist in any contacts objects)


- The Loop

As you are dealing with lots of objects, you will need to loop through them one by one. The best way to do this is by using the FOR loop. So as an initial start, create a for loop and loop through the objects based on contacts.length.

Code Hint No.1:

function lookUpProfile( firstName, prop ){
  for( var i = 0; i < contacts.length; i++ ){ 

  }
}

Tip: We are using contacts.length as it provides you with the precise number of objects inside contacts; it will ensure you will loop through all of contacts.


- 1st If Statement

The very first thing you need to check is if the passed firstName actually corresponds to any contacts objects. So we put this inside the loop because we need to check every contact firstName and try to find if it actually exists/matches.

Next,

If firstName does not correspond to any contacts then return “No such contact”

This is only true if you have gone through every single contact and none of them passed your if statement condition. In other words, if your For loop finished and firstName does not correspond to any contacts, that happened.

Hence you should return "No such contact"; outside of the loop; when the check is all over.

Code Hint No.2:

function lookUpProfile( firstName, prop ){

  for( var i = 0; i < contacts.length; i++ ){ 
    if( firstName == contacts[i].firstName ) {
      // do stuff here
    }
  }  // end of for loop
  
  return "No such contact";  // outside of loop, when all the search is done.
}

- 2nd If Statement

Now what we got left is:

  • Check if the property prop is a property that exists in the same contacts object which contains firstName.
  • Return the “value” of property prop if it exists inside the same contacts object which contains firstName.
  • Return “No such property” if prop does not exists inside the same contacts object which contains firstName.

Well, we can’t return the “value” of property prop if we don’t know if it exists or not.
So we need to check if the property prop is a property that exists in contacts object which contains firstName.

Let’s assume we found firstName within one of the contacts, so now we need to fill in the if statement block with a condition that is able to figure out if we have the property prop in the same contact object which contains firstName property.

Do you remember the challenge Testing Objects for Properties?

It used something really useful; object.hasOwnProperty(property).

The hasOwnProperty() method returns a boolean (true or false) indicating whether the object has the specified property as own property.

So this means if we checked:

contacts[i].hasOwnProperty(prop) === true

Will it return true if contacts[i] had the property prop?
Yes, it will.

Thus, we now need to nest an if statement inside our first if statement (which finds whatever if firstName property exist inside contacts[i] object or not). So that our nested if statement; which uses .hasOwnProperty(prop) === true as its condtion, will ensure we find if prop exists inside contact[i] which also contains firstName.

Also, while we are at it, let’s return contacts[i][prop] from that condition if it was true. Because that’s what the challenge wants.

  • Return the “value” of property prop if it exists inside firstName object.

Code Hint No.3:

function lookUpProfile( firstName, prop ){

  for( var i = 0; i < contacts.length; i++ ){ 
    if( firstName == contacts[i].firstName ) {
      if( contacts[i].hasOwnProperty(prop) === true) {
        return contacts[i][prop];
      }
    }
  } // end of for loop
  
  return "No such contact";  // outside of loop, when all the search is done.
}

- Last Statement

Now, we got one more thing left to do:

  • Return “No such property” if prop does not exists inside firstName object.

This translates to: If our nested if statement returns false, return "No such property".

So all you have to do is the usually else statement and return "No such property".
Because if your nested if statement could not find the property prop inside the object that has firstName, then “no such property” exists for that contact[i] object`.

Code Hint No.4:

function lookUpProfile( firstName, prop ){

  for( var i = 0; i < contacts.length; i++ ){ 
    if( firstName == contacts[i].firstName ) {
      if( contacts[i].hasOwnProperty(prop) === true) {
        return contacts[i][prop];
      } else {
        return "No such property";
      }
    }
  } // end of for loop
  
  return "No such contact"; // outside of loop, when all the search is done.
}

And that’s pretty much it.
Try to do the challenge without looking at the code hints or use them one by one as you wish.
Good luck.


#4

Hi folks, I don’t understand something about this code. I managed to solve it, however, I’m totally confused about why a function returns “No such contact” when there actually is! Can someone explain my mistake in logic here?
Here’s the code:

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

I know it’s wrong here and the other way round than instructions, however I’m rather curious about why is it like that.


#5

If you read the code step by step, here is what it is doing:

  1. Loop over all the items in the array (for)
  2. In the loop, see if the one we are currently on is the given name. If NOT, return “No such contact”. (if)

If the very first record in the collection is not the given name, it will immediately return. You have to allow the loop to go through the entire collection, and if it gets to the end of the collection, then return the message “No such contact”.

Hope this helps. :o)

Kenny


#7

It really helped, I was stuck for an hour or so :slight_smile: THANKS!


#8