0

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;
    }
 }  
}
3
  • 1
    This happens often enough there must be a dupetarget for it... Commented Jan 28, 2017 at 18:06
  • your title is not asking anything or implying of the problem. Commented Jan 28, 2017 at 18:09
  • Side note: If you find yourself writing if (a == b) { return true; } else { return false; }, stop and write return a == b; instead. It does exactly the same thing. Commented Jan 28, 2017 at 18:11

1 Answer 1

3

Think through the logic for a moment: What happens on the first loop? What does the function do in response to the if/else? Right! It returns true or false right away, without looping through the remaining entries at all.

You need to remove the else entirely and move return false to outside the loop:

function attempt(firstName) {
    for (var i = 0; i < contacts.length; i++) {
        if (contacts[i].firstName == firstName) {
            return true;
        }
    }
    return false;
}

Side note: Array#some is designed for exactly this use case:

function attempt(firstName) {
    return contacts.some(function(entry) {
        return entry.firstName == firstName;
    });
}
Sign up to request clarification or add additional context in comments.

7 Comments

@ibrahimmahrir - Please be careful when editing to pay attention to the "someone else has edited this" warning.
did you mean to blow away the ===? because without it, you can pass in 0 and match "". It's hard to imagine that's the intent as described in the question.
@pvg: == vs. === is irrelevant to the question. If you want to suggest a coding style, a comment would be the more appropriate place.
It's not irrelevant to the question, code presented can contain multiple problems, which this one does. It's not style, the expected behaviour as described in the question will not actually be matched by the code in this answer. In this case, 0 matching "" would not be a stylistic issue, it's a bug.
@pvg: You're presupposing that A) They're going to pass a non-string into the function (there's no indication of that), and B) They're going to have "" as first names in the array (there's no indication of that). Nothing in the question says anything about having other types floating around, and the function in the answer does exactly what the question says it should do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.