TypeError: cannot read property 'firstname' of undefined

OK so for the profile lookup challenge i wrote this code

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

// Change these values to test your function
lookUpProfile("Bob", "likes");

NOTE: it will work with all cases as long as there is a valid firstname input. So it will work
even with a invalid prop input.

Which I know it’s not as optimized as it could be.
Because as the amount of things you look through gets bigger it would get slower and slower
to return something but any way. I assume its throwing a problem in that its not ending the
for loop and returning “No such contact”. But I have no idea why. My console error is
TypeError: cannot read property ‘firstname’ of undefined
Any help or a different way of solving this challenge would be great!

This is an index out of bounds error. Remember that array indexing starts at 0 and as a result someArr[someArr.length] is undefined.

As pointed out by @ArielLeslie , the last iteration of the loop tries to get the element after the last element of the array . This element does not exist and hence it is undefined. And then the code tries to access the firstName property of an undefined object and therefore the TypeError: cannot read property ‘firstname’ of undefined

You can easily correct this by using < instead of <= .

Thanks! I forgot I did that.