Profile Lookup js

Profile Lookup js
0

#1

Tell us what’s happening:
what’s wrong in my code??

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 === firstName){
    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");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

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


#2

function lookUpProfile(name, prop){
// Only change code below this line
for(var i = 0; i < contacts.length; i++){
if(contacts[i].firstName === firstName){

It looks like an issue with your what you’re comparing contacts[i].firstName to.


#3

I’m having the same issue. In the instructions it says, ‘the function should check if name is an actual contact’s firstName’, but the function takes the argument name, not firstName. Is this a mistake or have I misunderstood something?

I wrote: if (contacts[i].firstName === name), but this doesn’t work.


#4

The function will take the argument “firstName” instead of name.
function lookUpProfile(firstName, prop){

};


#5

Nothing is wrong with your code. The problem that you are not looking into is how you are calling the Array!!!

example:

let arr = [a, b, c];
To access values, you write,
arr[0];
arr[1]
arr[2]
Right!

Now, let us look into more complex data.

let arr = [{ a: 1, b: 2, c: 3 }];
Now to access the values how you should write?
arr[0]['a']
arr[0]['b']
arr[0]['c']

Now understand the difference between each of these and re-write your for loop to pass the test.
And also you have to fix the for loop, And you can add comparison values in if else statements using && and ||

And don’t forget to close the topic with a solution.


#6

In that case I think there’s a problem with the way it’s set up:

function lookUpProfile(name, prop){
// Only change code below this line

Should be set up as:
function lookUpProfile(firstName, prop){

Otherwise you have to change to function name yourself.


#8

What is wrong with my code? @Randore @azhang213 @prashantverma22
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].firstName == name && !contacts[i].hasOwnProperty(prop)){
    return "No such property";
  }else if(contacts[i].firstName != name ){
    return "No such contact";
  }
}
// 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 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36.

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


#9

This one worked but I couldn’t get why the first did not work. @Randore @azhang213 @prashantverma22

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

#10

The first one didn’t work because let’s say you are calling the function with two arguments "Harry", and "lastName". Now during first iteration of the for loop it will get the first object with firstName with value "Akira". When it will reach the last else if it will compare "Akira" with "Harry" and since both doesn’t match the function will return "No such contact" and the for loop will terminate there and won’t check further. That’ts it. Let me know if something is not clear.