Profile Lookup - This lesson made me feel like I knew nothing

This challenge was extremely hard for me. I was aware I had to use a for loop, along with a (nested) if else statement. The thing that really got me was putting it all together. I wasn’t sure how to nest it together and am still wrapping my head around the for loop concept. I ended up finishing the challenge with the help of this forum and youtube videos, trying to make sense of each line one by one to make sure I understand it. I am slowly grasping it, but this challenge really made me feel bad about my progress. I see many others online were quite surprised at the challenge also. Others seem to get it with ease, and solved it in various different ways.

Just looking for some reassurance

Well, I was mostly looking for a walk through of the whole function we had to write. Some sort of explanation, because like I said, I watched a few videos and used the forum to try to understand it, but I just want to make sure.

  if (contacts[i].firstName === firstName) {
    if (contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else {
      return "No such property";
    }
  }  
}
  return "No such contact";

Also, now that I am thinking about it again, I really don’t understand the latter half. from the else statement to the end. Why is the second return outside of the brackets?

Oops, I forgot to post the for loop, but I do have that on my code. So essentially, that return statement is outside the brackets because it is the last possible scenario? If if (contacts[i].firstName === firstName) is not true, it continues onto the last return statement (which could be thought of as another else statement?). If it is true, it continues onto the next if statement - if that is not true, it returns "No such property";. So, I guess what I am asking is that you can look at this whole if statement as one whole if statement? Meaning you can only get to the second if statement if the first one is statisfied, since it is nested inside the brackets? The whole nesting if else statements through me off

if (contacts[i].firstName === firstName) {
    if (contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else {
      return "No such property";
    }
  }  

so the second if else statement is really just its own if statement, nested inside the first one

Yeah, after typing it out I realized that haha. Thank you for the help as well

That “style” of writing code is usually referred as early return and is widely considered a good practice to write code with that in mind.

The idea is to reduce the number of else to virtually 0, in order to have a better readability and less written code.

consider this simple add functions:

function add(a, b) {
 var sum;

 if(!a || !b) {
   sum = 'please define two numbers'
  } else if (typeof a !== 'number' || typeof b !== 'number') {
    sum =  'please only number'
  } else {
   sum = a + b;
}

return sum;
} 

Can be reduced in size by applying the early return pattern. Knowing that a function will execute from top to bottom and won’t read anything after a return statement we can simplify it as:

function add(a, b) {
  if (!a || !b) {
    return 'please define two numbers';
  }
  
  if (typeof a !== 'number' || typeof b !== 'number') {
    return 'please pass only numbers'
  }
  
  return a + b;
  
}

Hope it helps :slight_smile: