Trouble with Basic JavaScript: Profile Lookup

Tell us what’s happening:

Hello there!
Sooo, I got this code here. I do not understand what is going wrong… if you know what goes wrong, could you tell me? I did look at the solutions. I get the idea but I think I miss some formalities or lack some knowledge of rules.
Thanks for your help!

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 n = 0; n < contacts.length; n++) {

if (contacts[n].firstName === name && contacts[n].hasOwnProperty(prop)){
    return contacts[n][prop];}

else if (contacts[n].firstName !== name){
    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 (X11; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0.

Challenge: Profile Lookup

Link to the challenge:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0 .

I’m going to adjust your indentation so that it’s easier to see what is going on.

for (let n = 0; n < contacts.length; n++) {
  if (contacts[n].firstName === name && contacts[n].hasOwnProperty(prop)) {
    return contacts[n][prop];
  } else if (contacts[n].firstName !== name){
    return "No such contact";
  } else {
    return "No such property";
  }
}

The logic of this if statement is your problem.

What happens when you hit the very first contact that does not match name?

1 Like

Thanks! I will think about it! Guess there is more I did not get arrrr ))
Other question: Why did you change var to let?

Your code is pretty close. You just need to be careful with that if statement and make sure it’s doing what you want. (Correct return statements, but slightly off conditions)

I used let because it’s good practice. It’s not making a difference in your code but var can have some strange scoping issues. let is strictly restricted to the scope in which it was defined (in this case the for loop) while var is in scope for the entire function.

Ooooo I got it I think. I understood your first comment ))) thank you. My first code will always output “No such contact.” Haha. Allrighty then.

1 Like