Ana
Ana

Reputation: 80

If-else inside a for loop not working - Javascript

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".... :/

Upvotes: 1

Views: 5434

Answers (2)

Code Maniac
Code Maniac

Reputation: 37755

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"));

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44087

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"));

Upvotes: 1

Related Questions