Profile Lookup-

Tell us what’s happening:
I cant understand why my code is not returning anything the problem is asking for

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
if (contacts.hasOwnProperty(prop)){
for (i = 0 ;i < contacts.length; i++){
    if(contacts[i].firstName == name){
        return contacts[i][prop];
    }
    else{
        return "No such contact";
    }
}
}
else {
return "No such property";
}
// Only change code above this line
}

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/86.0.4240.198 Safari/537.36 OPR/72.0.3815.487.

Challenge: Profile Lookup

Link to the challenge:

You haven’t indexed the contacts array above the FOR loop so code ahead that line is not executing. That line should be inside the if-else condition to get the code executed right.

1 Like

I did it, but i still don’t understand why have to declare a variable for the code to work?

As soon as your code encounters a return, the function stops and exits. If this return is inside a loop the loop stops immediately.

Your logic above doesn’t really avoid this, because there are still return statements inside your loop.

Also, your contacts array will never have time property prop, only the individual entries in the array might.

1 Like

I did came up with another

for (let 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"

what i still don’t understand is why this doesn’t work if i dont make i a variable

undeclared variables throw an error stopping your code


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 it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Can you provide the full function please?

// 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 (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
}

lookUpProfile("Akira", "likes");

Let me add some formatting

function lookUpProfile(name, prop){
// Only change code below this line
  for (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
}

What errors are showing with this solution?

1 Like

it says “i is not defined”

Ah, that i know how to fix. You need to add let before you initialize the variable.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.