Basic Javascript: Profile Lookup Project

Hello! Im trying to solve the Profile Lookup on Basic Javascript. im not sure why its not passing. could someone give me hand??

this is my code …

//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 ){
    if(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
lookUpProfile("Akira", "likes");

Hey!
thanks for your answer.

I tried the brackets notation and also the dot notation, but i dont understand how can i access an item inside of the array without pointing to its position (contacts[i][1]). could you explain it to me?
Thanks

Hi, Thanks for the hints.

I tried to remake it, but still no luck.
This is my Code.

function lookUpProfile(name, prop){
// Only change code below this line
for(var i = 0 ; i < contacts.length; i++){
  if(contacts[i].firstName === name ){
    if(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
lookUpProfile("Akira", "likes");

First thing,
This line should be No such property instead of no such property.
Same goes for this line.

Second thing,

You have to use this return statement out of for loop.
Does this help?

Here is another way that i tried and i got a few marks correct.
That was the way i tought it at first but didnt think it woud work.
Still, its not returning the no such prop and no such content correctly , but idk why.


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(prop))){
      return "no such property";
    }
  }return "no such contact";
  
}
// Only change code above this line


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

Hi!
Thanks for your answer.

Unfortunately it didnt help. i really dont know whats wrong.

Could you post your latest code?

I changed the code , its giving me correct on almost everything except “Bob - Potato” , could you take a look!?

Thanks man!!

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(prop) === false ){
      return "No such property";
    }
    }
  
  return "No such contact";}
// Only change code above this line


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

Give the way you are using conditions,
You’ll need an extra condition here:

This condition should be such that,contacts[i].firstName === name.
See if that helps.

could you send me the correct code!? ive been in this hut for 2 days now and i have no idea how to solve it if not the 2 ways that i tried.

Thanks

function lookUpProfile(name, prop){
// Only change code below this line
for(var i = 0 ; i < contacts.length; i++){ //<- this loops through the array 
  if(contacts[i].firstName === name && (contacts[i].hasOwnProperty(prop))){// this means that if the first parameter submited on the function is equal to "firstName" AND it has a property listed in the array[i], it will return the requested parameters
      return contacts[i][prop];
    }else if(contacts[i].hasOwnProperty(prop) === false ){// if it doesnt have a listed property (2nd paramenter), it will return no such property.
      return "No such property";
    }
    }
  return "No such contact";} // if any of the past conditions doesnt pass, it will return no such contact.
// Only change code above this line


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

although i understand where it can be improved, im not sure HOW to improve it. i tried break in it into diferent ifs and else ifs but it didnt work either. I also tried making the positive and negative forms of each requested statement and it didnt work.

At this point im not sure whats wrong. please walk me through it.

You are failing this test-case right?
Let’s break down your code according to this condition.

If the objects in contacts array have the firstName as name and they have property prop.
Here, name Bob is not in any it thr objects of contacts array and there is no such property as potato.
So, it moves on to next condition.

In this condition, your code checks if the contacts[i] does not have a property named prop i.e. potato in this case and returns “No such property”.
This is right therr is no such property named potato but there isn’t even a name called Bob.
You are not checking that.
You will have to add that condition in this else if statement.
As i mentioned here:

Try doing that and you should be good to go.
Hope this helps.

1 Like

My approach could be:

  1. Instead of using for loop, I suggest to use the filter() method, which gets only the element that match with the name of the property ex: return contact.firstName === name;
  2. When you get the match with the filter you can assigned to a new variable and validate if that array exist or not, and with that information you can validate no such property or no such contact
if (filterName[0]) {
    return filterName[0][prop] ? filterName[0][prop] : 'no such property';
  } else {
    return 'no such contact';
  };
1 Like

@yamitrvg12 and @clevious, I agree with you guys.
But, the campers have to solve this one with for loop cause they have not been introduced to higher-order functions.