Profile Lookup - firstName not defined

Tell us what’s happening:
So first off, I’m trying to do this without the solutions and making my own code - and I’m currently stuck as the tests I’ve run keep coming back with “firstName not defined”. I’m not sure how to define it…or if I’m even on the right track. I’d love any and all help - I’m trying to make more sense of functions and how to write them…

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 && contacts[i].hasOwnProp(prop))
    return contacts[i][prop];
      break;
    } if
      (name !== firstName) {
      return "No such contact";
     
    } if
      (prop !== value) {
      return "No such property";
      }
// 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 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

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

Ok maybe I’m not understanding…I took out the break statement as that made sense but I’m really not sure why firstName is coming back as undefined. I know I’m missing something but I’m not sure what it is.

Ok as I’ve been researching and looking, that makes sense…so now I’ve gotten as far as passing the first 3 tests but then it comes up undefined again in the second round…am I adding in too much code?

So this is what I did:

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

Ok - again thank you! I feel like I’m super close but I’m not able to get a return for “no such property”. So here is the code I have up:

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];
     }
  {
    return "No such contact";
  }
    return "No such property";
  
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");

In your case since you’re using a for loop to find the contact by name, you should wait to return 'No such contact' until after you’ve exhausted the list and exited the for loop. Inside the loop you have the right idea, but I think you should split up your if statement. First check if you found a contact via contacts[i].firstName === name, then check if they have a prop called prop. If they do, you want to return the value stored there, if they don’t, well then you want to return 'No such property'.

Thank you for your help! By looking at it and walking away, I was able to figure it out – here’s hoping it makes sense as I learn more!