Profile LookUpFunction Halfway

Dear community!

I hope you can help me find a solution to creating the Profile LookUp function!

My code passes certain prerequesits (such as if the name is not in the contact list or the contact does not have a certain property) but fails at returning an existing property of an existing contact.

I post my code below and thank you for any hints!

function lookUpProfile(name, prop) {

  // Only change code below this line

for (let i = 0; i < contacts.length; i++)

{

  if (name !== contacts[i].firstName)

  {return "No such contact";}

  else 

  { 

    if (contacts[i].hasOwnProperty(prop))

    {return contacts[i][prop];}

    else 

    {return "No such property";}

  }

}

You should only return “No such contact” after checking every object and not finding a matching name, but this code only checks the first object.

@colinthornton I would have preferred if we’d help him figure that out instead of just telling him. Part of learning algorithms is figuring out how to find stuff like that out.

I was going to suggest logging out data to see what the problem is:


function lookUpProfile(name, prop) {
  console.log('\n\nentering function name =', name, ' and prop =', prop)
  for (let i = 0; i < contacts.length; i++) {
    console.log('\n* loop', i, ' contact =', contacts[i])
    if (name !== contacts[i].firstName) {
      console.log('* contact not found, returning no such contact')
      return "No such contact";
    } else {
      console.log('* contact found!')
      if (contacts[i].hasOwnProperty(prop)) {
        console.log('* prop found, returning value')
        return contacts[i][prop];
      } else {
        console.log('* prop not found, returning no such property')
        return "No such property";
      }
    }
  }
}

I would suggest trying that code with different inputs. Try to understand what the ones that pass all have in common.

I might also remind you that a return terminates the function. Once you hit a return, the function is over.

I would also suggest learning more standard JS formatting. You’re going to drive your coworkers crazy if you code like that. In reality, linters will catch it probably, but might as well save yourself some headache and learn it now.

So I somehow got it upside down… thanks for the input!

Thanks, I’ll work on my formatting!

With regards to formatting, you can right click on the code editor and select “Format my document” or something like that to automate it. Ideally you’d get used to that format and try to conform to it manually.

Thank you both, I somehow made it; If you have the nerves to give me hints on simplifying my code, I’d be very grateful!

function lookUpProfile(name, prop) {

  // Only change code below this line

  for (let i = 0; i < contacts.length; i++) {

    if (name === contacts[i].firstName) {

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

      else { return "No such property"; }

    }

  }

  return "No such contact";

  // Only change code above this line

}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

That seems pretty simple to me. I might use a ternary or short-circuit OR (||) in the middle there, but that’s my stylistic than simplification. I like the idea of using find to find the match. I think it’s sleeker and all things being equal I like to eliminate nesting where I can.

const lookUpProfile = (name, prop) => {
  const matchedContact = contacts.find(el => el.firstName === name)

  if (!matchedContact) {
    return 'No such contact'
  }

  return matchedContact[prop] || 'No such property'
}

To me, that is really easy to read and clear. But really it’s doing the exact thing your is, just using a method to find the value, but really, it’s just a disguised loop.

But yours work and does the job. Be happy with that and be proud that you solved it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.