Profile Lookup Help Pls

Profile Lookup Help Pls
0.0 0

#1

Okay. So I’ve searched the help forums on here and haven’t found any answers that help me. What am I doing wrong here?

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(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
  if (contacts.firstName === firstName){
    if (contacts[i].hasOwnProperty(prop)){
      return contacts[i].prop;
    }
    else {
      return "No such property";
    }
  }
} else{
   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.170 Safari/537.36 OPR/53.0.2907.99.

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


#2

You put your no such contact inside the for loop but actually you need to let the for loop keep searching for the name . That is, check if the name is not found after the loop has searched all the names unsuccessfully.


#3

This is my new code:

// Only change code below this line
for (var x = 0; x < contacts.length; x++){
    if (contacts.[x].firstName === firstName) {
        if (contacts[x].hasOwnProperty(prop)) {
            return contacts[x][prop];
        } else {
            return "No such property";
        }
    }
}
return "No such contact"; 
// Only change code above this line
} ```
It still isn't working. I've changed a few possible errors. Now, the console isn't even updating.  It just says the same old 'Your test output will go here' when I click submit so I can't even tell where I'm going wrong.

#4

In the first if statement you have contacts.[ which is wrong, remove the dot


#5

Now I’m getting “firstName is not defined”


#6

You modified the name of the first argument to the function even though you should not have touched any code other than the one marked by the comment area. Return it back to “name”


#7

Still getting “firstName not defined”.
I put “let firstName = name” and it accepted it but I’m not so sure that it actually makes sense in a real scenario.


#8
//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
let firstName = name;
for (var x = 0; x < contacts.length; x++){
    if (contacts[x].firstName === firstName) {
        if (contacts[x].hasOwnProperty(prop)) {
            return contacts[x][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");

This is what it finally accepted.


#9

No just put back the original function parameter. Instead of lookUpProfile(firstname, prop) change it back to name which is the original code and fix the rest of your code to use name as the argument.


#10

That did it. In my first if statement I had it set === to firstName when it should’ve been “name” like you said. I think that dot that I had earlier was the real issue and I changed too much around trying to figure out what was going on and messed the whole thing up. Thank you for your help (and patience!).