[Solved] trying to understand this profile lookup in JavaScript


I am sharing my solution which is slightly different from yours. Compare it to your own. You will see that I only return inside my for loop when I get a positive match , otherwise I let the loop run. This is the biggest difference.
You need to let the loop run fully and then through some mechanism keep track of the missing conditions . I have used two different variables to track the missing conditions here.

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

0

solved trying to understand this profile lookup in JavaScript