samedi 28 janvier 2017

trying to use a for loop with if else statement in objects

I'm trying to write a function that will iterate through a variable holding objects. If you pass in a first name that is an object property, you should get true. If not, you should get false. However, no matter what I pass through the function, I always get false. Any help is greatly appreciated.

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 attempt(firstName){
for(var i = 0;i < contacts.length; i++){
    if(contacts[i].firstName==firstName){
    return true;
    } else {
      return false;
    }
 }  
}

Aucun commentaire:

Enregistrer un commentaire