Basic Javascript: Profile Lookup Project

Basic Javascript: Profile Lookup Project
0

#1

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");

#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

Also, in the future, make sure to select the JavaScript category for questions about any of the JavaScript challenges.

Thank you.


#3

Above you have used bracket notation around firstName, so JavaScript expects firstName to be a variable name in your code. It is not, so you get a Reference Error that firstName is not defined.

There is another way to reference a property name if it already exists. I suggest reviewing some of the challenges which involve accessing object properties to refresh your memory.


#4

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


#5

contacts[i] is an item of the array. It happens to be an object. To reference a property name of an object you can reference it in two ways, but which way depends on the property name itself. Review these previous challenge below and should you be able to figure out how to reference the firstName property correctly.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-dot-notation

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-bracket-notation

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables


#6

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");

#7

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?


#8

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");

#9

Hi!
Thanks for your answer.

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


#10

Could you post your latest code?


#11

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");

#12

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.


#13

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


#14

Show us what you have tried again and we will guide you to a solution.


#15
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.


#16

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.


#17

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';
  };

#19

@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.


#22

#23

@yamitrvg12, @aditya_p I am closing this thread, because it has diverged from explaining how to help the OP and into just posting solutions. Please do not post full working solutions. Give the OP a chance to reply back and let us know if a solution was made.