Loops in Profile Lookup Challenge

Tell us what’s happening:
I was working on the profile lookup test (https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup) and I kept running into a problem.

My code would (correctly) return the correct [prop] for a given name, and it would also correctly return a “No such property” string.

However, when I added some code to return “No such contact”, the “No such contact” message would override code that was previously working, regardless of the order of my else if statements.

I later discovered that it was caused by my “No such contact” code being placed inside the loop. My question is: how does this happen? I thought that the loop was supposed to stop as soon as a true condition was reached and that further actions weren’t taken.

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(name, prop){
// Only change code below this line
for(let i = 0; i < contacts.length; i++)
{
   if(name == contacts[i]["firstName"] && contacts[i][prop] !== undefined)
{
return contacts[i][prop];
}

else if(name == contacts[i]["firstName"] && contacts[i][prop] === undefined)
{
    return "No such property"
}

else if (name !== contacts[i]["firstName"] && contacts[i][prop] == undefined)
{
return "No such contact"
}
}
    
   
// Only change code above this line

}
console.log(contacts[0]["firstName"])
console.log(lookUpProfile("Bob", "potato"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0.

Challenge: Profile Lookup

Link to the challenge:

Well your code looks right but this part is unecessary

at the end. Because since that is the last one and the last part only runs when none of the other ones are true, you shouldn’t need to put anything but return.

So when this one is not true then it goes to this one,

Then if that’s not true that means there is not such thing in the contacts, so you just return because none of the parameters are similar from the one inside the object array.

So now

for(let i = 0; i < contacts.length; i++)
{
   if(name == contacts[i]["firstName"] && contacts[i][prop] !== undefined)
{
return contacts[i][prop];
}

else if(name == contacts[i]["firstName"] && contacts[i][prop] === undefined)
{
    return "No such property"
}
return "No such contact"
}

you don’t need to use this one because none of the other ones are true. It’s unecessary and might cause a bug