2

I amm new to javascript and am working through freecodecamp's resources (amongst other things). There was a challenge task to loop through phone contacts and I am sure some people will be familiar with it. I managed the task just fine, but am confused as to why the final return statement needs to exist outside the for loop, as opposed to an else statement for the outer if.

function lookUpProfile(name, prop){
  for (var i = 0; i < contacts.length; i++) {
    if (name === contacts[i].firstName) {
      if (prop === "lastName" || prop === "number" || prop === "likes") {
        return contacts[i][prop];
      }else{
        return "No such property"
      };
    }    
  }
  return "No such contact";
};

If I ever nested the final return statement (as an else) inside the for loop, the code would appear to go straight to this, even though the conditions for the initial if statement were true.

Can anyone please explain why? If you need any further information please let me know. I have tried searching google for why this might be the case but I haven't managed to find anything.

Thanks!

4
  • If it's inside the for loop it will return No such contact if name === contacts[1].firstName. You have to look through all of the items to make sure that the one you are looking for is not among them. Commented Oct 15, 2019 at 6:53
  • I don't have a problem digesting your code. The final return statement just covers the case where either the input name does not match to any contact, or there are no contacts available. Commented Oct 15, 2019 at 6:54
  • What do you want the code to do if the name matches but prop isn't one of those three values? I can't make out what the logic is meant to be. Also, if prop has a known set of three values, why not check it before even starting the loop? Commented Oct 15, 2019 at 6:55
  • If you moved the return inside the for loop, the function would exit when hitting the first non-match. The function would never find anybody unless they happen to be first in the list. Commented Oct 15, 2019 at 6:56

3 Answers 3

1

You don't wanna return "No such contact" just because comparing first element returns false. This will not work. Only after comparing and checking for all the elements in array you would know the contact doesn't exists. If the first contact doesn't match loop will return and no further comparisons will be done.

Sign up to request clarification or add additional context in comments.

Comments

1

I've added an else to your example to be more explicit to hopefully make it more clear what is actually happening in the for loop. That else statement doesn't need to be there because the for loop would continue regardless.

function lookUpProfile(name, prop){
    for (var i = 0; i < contacts.length; i++) {
        if (name === contacts[i].firstName) {
            if (prop === "lastName" || prop === "number" || prop === "likes") {
                return contacts[i][prop];
            }
            else return "No such property";
        } else {
            continue;
        }  
    }
    return "No such contact";
};

If you were to place a return where the continue currently is you would never make it past the first contact.

Comments

0

You need to iterate over all the elements to make sure that there is no contact with matching name.

Comments

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.