Profile Lookup error

Profile Lookup error
0

#1

Tell us what’s happening:
"TypeError : cannot read property ‘0’ of undefined"
what does that mean what is wrong with my code?

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 x = 0; x < contacts.length; x++){
      if(contacts.firstName[x] === 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", "address");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Linux; U; Android 4.2.1; en-gb; ME301T Build/JOP40D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30.

Link to the challenge:


#2

The line with the error is:

if(contacts.firstName[x] === firstName){

I believe you just made a typo (x is out of order), because you seem to realize contacts is an array throughout the rest of your code. In this particular line, you are treating contacts as a plain object and firstName as an array.


#3

Probably this is what is causing it contacts.firstName[x] , because contacts.firstName is not an array, while contacts is , I notice that you are referring to it correctly below that line so it may just be a typo.


#4

i dont understand what should i do then?


#5

Look at how you are correctly referencing contacts’ elements in the if statement below the line with the error.


#6

oh should i just write contacts[x] instead of contacts.firstName[x] because that is not working either


#7

I could be wrong, but I get the feeling you did not write all of the other code. Do you understand what contacts[x] represents at any given iteration of the for loop?


#8

at this point i dont even know what my name means so no.


#9

Put a console.log(contacts[x]) on the line before the if statement and look at your browser’s console (Ctrl+Shft+J in Chrome), so you can see what it looks like at the first iteration.


#10

it says no such contact


#11

If your code looks like the following, then you will see something in your console and it will not be “no such contact”

function lookUpProfile(firstName, prop){
// Only change code below this line

  for(var x = 0; x < contacts.length; x++){
      console.log(contacts[x]); // this is the line I wanted you to add
      if(contacts.firstName[x] === 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
}

#12

yeah i did that exactly and it says no such contacts


#13

now its giving the above error again


#14

try

if (contacts[x].firstName === firstName){

if (contacts[x].hasOwnProperty(prop)){
  return contacts[x][prop];

#15

I was trying to get the OP to figure it out without giving the solution.


#16

I understand, but all that was doing was confusing him.