Profile Lookup - JS

Hello! I am stuck at this challenge for a while now. I entered to challenge guide to see some hints but I ended up seeing the solutions and somehow understood. But all I need is someone to explain me why my code isn’t working.
That’s what I wrote:

 function lookUpProfile(name, prop) {
         if (name !== "firstName") {
               return "No such contact";
        for (var i = 0; i < contacts.length; i++) {
                if (contacts[i]["firstName"] === name) {
                         for (var j = 0; j < contacts[i].length; j++) {
                                  if (contacts[j] === prop) {
                                            return "value";
                                  } else if (contacts[j] !== prop) {
                                            return "No such property";

Probably I wrote something really bad… Sorry to waste your time!
Thank you! Have a great day!

Hi @theona2212 !

Welcome to the forum!

I have to log off but I added the challenge link so people know what lesson you are on.

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 (’).

1 Like

Thank you for helping me understand the settings better and taking from your time to edit my comment. I appreciate!

starting from here, consider really carefully, could ever name be equal to the string "firstName"?

Hey theona,

I think you misunderstood the task a little bit.

The algorithm must solve two problems:

(1) You need to go through the list of contact entries and find the entry that uses the same value as firstName as your parameter name. If there is no such entry => return “No such contact”.

(2) You found the matching contact in (1). Now you have to check whether the contact entry holds a property who’s identifier matches the value of prop.

If yes => return the value for that prop
If no => return “No such property”.

Here a few hints:

  • Have a look at the Javascript “find” function to solve the (1) step. Save the result into a local variable
  • You may look into the “hasOwnProperty” function or the “in” operator to solve (2).

Finally what is wrong with your code:

(1) comparing name with the string literal “firstName” does not achieve anything
(2) Your code to find the matching entry by “firstName” looks ok, but your inner loop does not do what the algorithm asks.

And a few code style suggestions:

  • Try to avoid the for loop using indezes. JavaScript provides lots of great functions for arrays such as find, map, filter etc. If you still want to use a for loop then consider using forEach or for … in.

I hope this helps you a bit.

Keep coding!


1 Like

Thanks for pointing that, now I can see a clearly mistake.

I see that I wasn’t very careful about the requirement on this challenge. I really misunderstood the string "firstName" with name
Thank you for taking your time explaining me! I’ll look up on how to improve my skills regarding JS and not only.
But may I ask you a question? Is there another way to write this code other than the given solutions?

I ended up using the in operator and the let function.
I understood all the mistakes I made and learnd new concepts. I’ll look up to learn more things that JS provides.

Thank you for helping me!

1 Like

Hi theona2212,

oh, definitely. You will often find out that there are a million possible ways to solve an algorithm problem but only a handful that make sense. That’s why you need to practice, to find this handful of solutions.


1 Like

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