Javascript - Profile lookup

I have tried everything. Nothing worked. Can someone explain me what is wrong with my code?
Thanks

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup/

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

I think the first problem is this else statement. Basically your code says to return “No such contact” if the name provided doesn’t match the firstname of the first record. What you want is to return “No such contact” only after the for loop has failed to find any matching firstname.

Here’s an example of a bad for loop simplified:

for (var i=0; i < contacts.length; i++) {
if (name === contacts[i].firstName) {
//yes! found the name!
return “Found You!”;
} else {
//hmm! that’s not the correct name. I’m going to quit now!
return “No such contact”;
}

And here is some pseudocode that explains what you could do instead:

//define some variable and set it to false
for (var i=0; i < contacts.length; i++) {
if (name === contacts[i].firstName) {
//yes! found the name! Now do something interesting
//but also, let’s set our variable to say we found the name by setting it to true
} //end if
} //end for
//now I’m outside the for loop, let me see if the variable I set earlier is true or false
//if it is still false, that means my for loop never found the name

hope this helps.
./H.B.

Hi @Randore and @hbar1st. The below listed are the test cases ,

“Kristian”, “lastName” should return “Vos” - failed
“Sherlock”, “likes” should return [“Intriguing Cases”, “Violin”] -failed
“Harry”,“likes” should return an array -failed
“Bob”, “number” should return “No such contact” - passed
“Bob”, “potato” should return “No such contact” -passed
“Akira”, “address” should return “No such property” -passed

The below mentioned line of code should return the success result for the first three test cases

 if(contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
      }

I have tried console.log( return contacts[i][prop]) if the condition is true. The result it produce is very unexpected and odd. It gives the results for akira.

Here is the repl.it link

Thank you for your time

Hi @JM-Mendez . I have tried all the combination came in my mind.
steps I followed

  1. Declared a variable called value.
  2. If the condition is met the value variable is updated and break statement is used to exit the for loop.
  3. Still the last function call is overriding the results and I am not getting the expected results.
  4. When comment all the test cases except one I get the expected output but when I uncomment all the test cases only the final function output is displayed.

Can you help me out, please

Thanks

After spending more time with your code, I noticed something I missed before. The final return is inside the loop, so will fail every time if the final item matches no contacts. Try moving it out of the loop.

function lookUpProfile(name, prop){
// Only change code below this line
  for(var i = 0; i < contacts.length; i++){     
   if(name === contacts[i].firstName){       
       console.log(contacts[i].firstName, prop, contacts[i].hasOwnProperty(prop));
      if(contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
      }else{
        return "No such property";
      }
    }else{
    // this line runs for every item in the array
      return "No such contact";  
    }
  }
// Only change code above this line
}