Profile lookup for loop (spoiler alert)

Hi all.

I am running on a problem with profile lookup challenge.
Here the working solution:

function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) { 
  if (contacts[i].firstName === firstName) {
    if (contacts[i].hasOwnProperty(prop) === true) {
      return contacts[i][prop];  
      }      
  } 
  else if (contacts[i].hasOwnProperty(prop) === false) {
      return "No such property";
    }
} return "No such contact";  
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

ALL GREEN!!! yuuhuu

here what I would write:

function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) { 
  if (contacts[i].firstName === firstName) {
    if (contacts[i].hasOwnProperty(prop) === true) {
      return contacts[i][prop];  
      }      
  } 
  else if (contacts[i].hasOwnProperty(prop) === false) {
      return "No such property";
    }
else if (contacts[i].firstName !== firstName) {
      return "No such contact";
    }
}   
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

“Kristian”, “lastName” should return “Vos”
“Sherlock”, “likes” should return [“Intriguing Cases”, “Violin”]
“Harry”,“likes” should return an array

WRONG!!

What I can’t understand is why the return result for “No such contact”; must lives outside the for loop.

Thank you if you’ll give me an explanation

have fun and code safe

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

1 Like

Hi, I am also working through FCC. Well done for getting so far! I feel I need some encouragement, maybe you do too!

You asked about why the last return should be outside the for each’’’ loop?