Basic JS: Profile Lookup tests problem

Basic JS: Profile Lookup tests problem
0.0 0

#1

Hello, campers;) Have a problem, need an advice. Can’t pass the tests, but code is work correctly (tested in chrome). My code from task.

for (var i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
    return contacts[i][prop];
  } else {
    return "No such property";
  }
}
for (var i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName !== name) {
    return "No such contact";
  }
}

#2

For the test case of lookUpProfile(‘Kristian’, ‘lastName’), the above for loop only looks at the first contact object. The if statement checks to see if object’s firstName property (“Akira”) is equal to “Kristian” and if the object has a property named “lastName”. Because both of the conditions must evaluate to true (because of the && operator), the if statement evaluates to false because “Akira” is not equal to “Kristian”, so your else block of code executes and returns “No such property”. Once a return statement has executed, the function is immediately exited and no further function code is processed.


#3

Got it. I’ll try some other methods. Thank you!