1

I have an array "contacts" with a few properties. My function uses a for to check if the firstName property of each contact matches the name parameter of my function, if it does, it checks if such contact has a property that matches the prop parameter (an if inside the previous if). Both "ifs" have corresponding "elses": "no such property", "not such contact".

Code is very simple actually:

var contacts = [
  {
    "firstName": "Akira",
    "lastName": "Laine",
    "number": "0543236543",
    "likes": ["Pizza", "Coding", "Brownie Points"]
  },
  {
    "firstName": "Harry",
    "lastName": "Potter",
    "number": "0994372684",
    "likes": ["Hogwarts", "Magic", "Hagrid"]
  },
  {
    "firstName": "Kristian",
    "lastName": "Vos",
    "number": "unknown",
    "likes": ["JavaScript", "Gaming", "Foxes"]
  }
];


function lookUpProfile(name, prop) {

  for (var 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";}
  }  
}

console.log(lookUpProfile("Harry", "likes"));

Following lines work perfectly:

console.log(lookUpProfile("Akira", "likes"));
console.log(lookUpProfile("Akira", "lala"));

Now, If I put any other firsNames of the rest of the elements:

console.log(lookUpProfile("Harry", "likes"));
console.log(lookUpProfile("Kristian", "likes"));

it returns "No such contact".... :/

1
  • you are using return within for loop, which will interrupt your for loop when no match is found. Commented Jul 7, 2019 at 5:12

2 Answers 2

1

That's because you're returning No such contact in the first loop iteration. Place this at the bottom of your function after the loop as a catch-all.

var contacts=[{"firstName":"Akira","lastName":"Laine","number":"0543236543","likes":["Pizza","Coding","Brownie Points"]},{"firstName":"Harry","lastName":"Potter","number":"0994372684","likes":["Hogwarts","Magic","Hagrid"]},{"firstName":"Kristian","lastName":"Vos","number":"unknown","likes":["JavaScript","Gaming","Foxes"]}]

function lookUpProfile(name, prop) {

  for (var 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";
      }
    }
  }
  return "No such contact";
}

console.log(lookUpProfile("Harry", "likes"));
console.log(lookUpProfile("Kristian", "likes"));
console.log(lookUpProfile("Made Up Person", "likes"));
console.log(lookUpProfile("Akira", "Made Up Property"));

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

3 Comments

Mmm but I don't understand... why outside..? Why can't it be inside the else of the first if... ? Isn't that logic? And why the for stops there.. I can't code if i don't understand the way it works..
return exits the function - so if the first contact isn't the person you're looking for, then it'll flow to the if, right? You were returning - and that will exit the function with the message No such contact. Does that make sense @Ana?
Ohhh I seee, Guess I need a lot to learn yet :D Thank u very much!
0

In your current code if the first element of array doesn't match the current passed name than it goes to else block and return "No such contact" ( eventually just checks first element only )

You're returning wrongly from else block in for loop, you need to place it out of loop

var contacts = var contacts=[{"firstName":"Akira","lastName":"Laine","number":"0543236543","likes":["Pizza","Coding","Brownie Points"]},{"firstName":"Harry","lastName":"Potter","number":"0994372684","likes":["Hogwarts","Magic","Hagrid"]},{"firstName":"Kristian","lastName":"Vos","number":"unknown","likes":["JavaScript","Gaming","Foxes"]}]

function lookUpProfile(name, prop) {
  for (var 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";
      }
    }
  }
  return "No such contact";
}

console.log(lookUpProfile("Harry", "likes"));

3 Comments

Mmm but I don't understand... why outside..? Why can't it be inside the else of the first if... ? Isn't that logic? And why the for stops there.. I can't code if i don't understand the way it works..
@Ana it is outside as if none of the contact matches in loop then only return no contact found, if there's any matching contact your function don't event come to this line
@Ana In your current code if the first element of array doesn't match the current passed name than it goes to else block and return "No such contact" ( eventually just checks first element only ) this line explains why you need to place it out of loop

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.