Profile Lookup - firstName not defined

Profile Lookup - firstName not defined
0

#1

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


#2

If I properly format/indent your existing code, it will help you to better see what the issues are.

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
}
  1. You do not need the break statement, because the return causes the function to exit as soon as it is encountered.

  2. Because the return statement exits the function immediately when it is encountered, your for loop only makes a single iteration and ends up returning the first contact’s prop property value (if it exists). If it does not exist, it will return undefined. When the function is exited, the for loop stops.


#3

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.


#4

Let’s talk about the above if statement.

  1. You reference a variable named firstName, but you have never defined a variable named firstName in your code.

  2. There is no such method as hasOwnProp, so you need to research what valid object methods exist to accomplish what you are trying to evaluate here.


#5

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?


#6

So going back to the if statement above, what are you trying to accomplish with the following? Since you have not defined firstName, can you think of another variable which is defined that could go here instead?

contacts[i].firstName === firstName

#7

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";
  }

#8

The linter used by the FCC test suite expects you to use { and } for if statement code blocks, so if you what that return statement to execute, you need to wrap it in { and }.

Also, make sure you always post ALL of your code, which includes the function declaration and closing }, so we can make sure exactly what you have.


#9

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");

#10

I’ve edited your post a couple of times now 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 easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

You still have not wrapped your if statement code block in { and }.

Also, you now have no logic for the 2nd and 3rd return statements. Can you explain in plain words (no code), when you think you should return “No such contact” vs. “No such property”? The, can you think about how you will check for those two conditions with code?


#11

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'.


#12

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!