Profile lookup while loop

Profile lookup while loop
0

#1

Can’t seem to make this one work with while loop

These criterias don’t work:

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

these do:
“Bob”, “number” should return “No such contact”
“Akira”, “address” should return “No such property”

function lookUpProfile(firstName, prop){
// Only change code below this line

 var i = 0;
 var target;
  
  while (i<contacts.length) {
    target = contacts[i];
    if (target.firstName === firstName) {
       if (target.hasOwnProperty(prop)==1){
         return target[prop];
       }else{
         return "No such property";
       }
    }else{
    return "No such contact";
    }
  i++;
     
  }
  
// Only change code above this line
}

any help is appreciated


#2

First of all, why are you using a while loop here? It certainly can be used, but this is basically what a for loop is built for.

Regarding your error, make sure that you are returning “No such contact” in the right place.


#3

Thanks RadDog25!

Just thought I’d want to try it with while.

Moved return "No such contact"; outside of the while loop.
Works now.

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

// Change these values to test your function
lookUpProfile("Kristian", "lastName");