Iterate Through an Array with a For Loop : Why do I need to exit the For-Loop?

can someone explain why this solution would not work for the challenge? Why do I need to exit the For-Loop if the first If condition isn’t met? Why can’t I just return ‘No such property’ with an Else statement within the For-Loop?

For example if I run the function lookUpProfile(“Kristian”, ‘lastName’), the input “Kristian” meets the criteria for the 1st If-statement and then moves down to the nested If-statement and then finally execute and return contacts[i][prop]? Why am I getting ‘No such contact’ even though the 1st If-statement’s criteria is met?


//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){
    for (let i = 0 ; i < contacts.length; i++){
        if (contacts[i].firstName === name){
            if (contacts[i].hasOwnProperty(prop)){
                return contacts[i][prop]
            } else {
                return 'No such property'
            }
        } else {
            return 'No such contact'
        } 
    }
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop

I’ve edited your post 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.

markdown_Forums

1 Like

The link that you provided took me to a totally different challenge to the one that you are trying to solve.

I misread it the first time. In your loop if the first contact that is checked fails it will end the loop. Therefore you are never actually getting down to the last contact. You want to move your return of No such Contact outside of your loop.

1 Like

thank you for the quick reply! so just to solidify my understanding…if the contact with “firstName”: “Kristian” was the only contact in the contacts array or if it was the 1st object in the array then lookUpProfile(“Kristian”, “lastName”) would execute properly with the function I wrote because it would pass the 1st If-Condition?

Test it and find out :smiley: (That’s not me being glib, testing a hypothesis really is a very helpful part of learning and problems solving)

1 Like

Yup. Done ditto. Discussing and reiterating helps me retain new information :smiley:

Thanks for the help! Brand new to this forum and already loving the community’s enthusiasm.