Profile Lookup stuck in logic

Here is my code:

function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
  if ((contacts[i].firstName === name) && contacts[i].hasOwnProperty(prop)) {
    return contacts[i][prop];
  else if (contacts[i].hasOwnProperty(prop) && name!=contacts[i].firstName) {
    return "No such contact";
  else {
    return "No such property";
// Only change code above this line

I realise the problem is that the code gets stuck on the first object (it does not itterate through all the objects) as it is giving wright answers only for the first object in the array. I don’t understand why. I thought everything in the curly brackets is executed for every instance of i.

As a clarification I have seen the wright solution and I just don’t get where is the difference and how JS executes the bits of code. I would be very gratefull if someone would point me to where I could learn algorithms in JS (I think this might be my problem). Also excuse my english as it is not my native language. Thanks.

Your function has a loop inside it, the loop execute for the first time, and this code runs with i having a value of 0

As you have a chain of if ... else if ... else ... one of them is always executed, and a return statement exit the function, as such your loop will never go to the iteration with i having a value of 1

You need to rethink a bit the if statements and also you may need to think of moving one of them outside the loop