Profile Lookup challenge, can someone give me a tip? SOLVED

function lookUpProfile(firstName, prop){
// Only change code below this line
var i = “”;
var k = “”;
var result = “”;

// loop through the array of profiles
for (i = 0; i < contacts.length; i++) {

// if the firstName parameter is equal to the firstName key in the object, carry on to next step, else return no such contact.
if (contacts[i].firstName == firstName) {

// the name matches, now loop through the object keys to find if the prop parameter matches.
for (k = 0; k < contacts[i].length; k++) {

    //if the prop parameter matches a key in the object, result is equal to the key's value
    if (contacts[i].hasOwnProperty(prop) === true) {result = contacts[i][prop];}
      
    //if the prop matches nothing result is equal to no such property.
    else {result = "No such property";}
      
    }
}

else {result = "No such contact";}
  
}

//return the result of our search
return result;
// Only change code above this line
}

// Change these values to test your function
lookUpProfile(“Sherlock”, “lastName”);

1 Like

What is the error you get?

I formatted your code below to make it easier to read. The problem is on the line containing:

for (k = 0; k < contacts[i].length; k++) {

contacts[i].length evaluates to undefined, so this for loop never runs. contacts[i] refers to an object contained in the array contacts. Objects do not have a length like an array. You can get rid of the for loop and keep the if…else statement inside of it. See if this helps you solve the problem.

function lookUpProfile(firstName, prop){
  // Only change code below this line
  var i = "";
  var k = "";
  var result = "";

  // loop through the array of profiles
  for (i = 0; i < contacts.length; i++) {
    // if the firstName parameter is equal to the firstName key in the object, carry on to next step, else return no such contact.
    if (contacts[i].firstName == firstName) {
      // the name matches, now loop through the object keys to find if the prop parameter matches.
      for (k = 0; k < contacts[i].length; k++) {
        //if the prop parameter matches a key in the object, result is equal to the key's value
        if (contacts[i].hasOwnProperty(prop) === true) {result = contacts[i][prop];}
        //if the prop matches nothing result is equal to no such property.
        else {result = "No such property";}
      }
    }
    else {result = "No such contact";}
  }
  //return the result of our search
  return result;
  // Only change code above this line
}
1 Like

With this code, it will end up returning the “no such contact” string for every entry.

I got rid of the for loop that looped through the object (since you told me how they arent numbered like an array.) though now im still only getting the “no such contact”

ive tried many slight variations in my syntax and i simply can’t figure it out.

i solved this by getting rid of my second for loop, then adding individual return statements for the result variable. i added them in the if/else statement nested within the first if statement, putting them as the last statement for both the if and else paths.

Would you mine posting your final solution? This could help someone else.