Profile Lookup Challenge: My code looks too long

Hello guys, I’m brazilian and my english isn’t very goog yet. I feel worry with my code because it seems too long. I tried to solve this challenge alone without using hints and this was the result. Only after solve it I saw the hints on forum and I was surprised with size of code solution: very small when compared to mine! I would like to know if there be hope to me.

function lookUpProfile(name, prop) {
  for(let i = 0; i < contacts.length; i++){
    if(contacts[i]['firstName'] === name && contacts[i].hasOwnProperty(prop) === true){
      return contacts[i][prop]
    }else if(contacts[i]['firstName'] !== name){
      if(contacts[i + 1]['firstName'] !== name){
        if(contacts[i + 2]['firstName'] !== name){
          if(contacts[i + 3]['firstName'] !== name){
          return "No such contact"
          }
        }
      }
    }else if(contacts[i].hasOwnProperty(prop) === false){
      return "No such property"
    }
  }
}

Yes, you shouldn’t be checking the contacts 1 by 1 - you should be using a loop. What if they had 5 contacts? 50? 500?

You can use a loop, or there is a fancy array prototype method that would make it even sexier.

1 Like

If your code is a working solution to a curriculum problem, it should be wrapped in [spoiler][/spoiler] tags, which I did for you.

I would like to know if there be hope to me.

First of all, small doesn’t necessarily mean better. Easy to read and maintain are often more important.

And of course there’s hope. Why wouldn’t there be? This is how you learn. You’ve written a messy, non-ideal solution. I wrote a lot of those when I was learning. Now I do this for a living. Just keep at it, you’ll get there.

1 Like

This is not needed. You are already iterating through the contacts array. What if the contacts array had 1,000 objects in it? Would you really want to write 999 if statements. Rethink how you could solve this challenge if you did not know how many objects were in contacts.

1 Like

I tried before, since yesterday, but without sucess. After make some notes with pen and paper I had this idea of solution that was not the better, surely.

Sorry by spoiler, I’m learning work on this. And thanks a lot by your words and advices, helped me a lot. May God bless you!

It’s true. Unfortunately I cannot try to solve this challenge with no hints because I saw them after finished my solution. Thanks a lot by your help and advice. May God bless you!

When you post a challenge, please always post a link so we don’t have to search for it. The Get Help -> Ask for Help button will do the same.

You want a hint?

OK, I got excited when you mentioned “pen and paper” - writing things out can be a great way to work on algorithms. Don’t worry about JS for a minute, just think about the process. How would you formally explain it to someone. If someone asked me for getting the groceries out of the car, I would say:

unlock car
while there are still groceries
  if there are frozen groceries
    take to the freezer
  else if there are refrigerated groceries
    take the refrigerator
  else
    take to the pantry
lock car
get kiss from wife for being a good husband

That is my algorithm for how to unload the car.

Can you come up with an algorithm for this and write in pseudocode? Don’t worry about JS and don’t worry about being “correct”, just right something down. Then we can point out if there is an issue.

If you were explaining how to do this to someone that wasn’t smart but was really, really, really good at taking specific directions (a computer), how would you tell them to do this?