Basic JavaScript - Profile Lookup

Tell us what’s happening:

Alright, I have checked multiple solutions regarding this and while I do understand those solutions, I want to know what exactly is wrong with my own. The code I did is somewhat similar to how I solved the Record Collection challenge, with the exception of using a for loop now.

Note that the challenge says the last 3 (of 6) tests pass just fine, with “No such contact” or “No such property” as solutions. There may be a bit of redundant code here too, but still I think it should work.

Your code so far

// Setup
const contacts = [
  {
    firstName: "Akira",
    lastName: "Laine",
    number: "0543236543",
    likes: ["Pizza", "Coding", "Brownie Points"],
  },
  {
    firstName: "Harry",
    lastName: "Potter",
    number: "0994372684",
    likes: ["Hogwarts", "Magic", "Hagrid"],
  },
  {
    firstName: "Sherlock",
    lastName: "Holmes",
    number: "0487345643",
    likes: ["Intriguing Cases", "Violin"],
  },
  {
    firstName: "Kristian",
    lastName: "Vos",
    number: "unknown",
    likes: ["JavaScript", "Gaming", "Foxes"],
  },
];

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

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

  // Only change code above this line
}

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Profile Lookup

Have you looked at what function returns for failing test cases?

console.log(lookUpProfile("Kristian", "lastName"));
console.log(lookUpProfile("Sherlock", "likes"));
console.log(lookUpProfile("Harry", "likes"));

All of these print No such contact. Any ideas why that might happen?

1 Like

// running tests

lookUpProfile("Kristian", "lastName")

should return the string

Vos
lookUpProfile("Sherlock", "likes")

should return

["Intriguing Cases", "Violin"]
lookUpProfile("Harry", "likes")

should return an array // tests completed

1 Like

:balloon:Hello @Albedo! Welcome to the forum!

This is a tricky challenge. The for loop limit you specified follows.

i <= array.length

could be problematic.

Does this help?
Keep up the good progress!

Happy Coding! :slightly_smiling_face:

1 Like

Of course, but function doesn’t do that. There’s only one place in function where No such contact is returned. Take a closer look there. Does the condition looks right, does returning at that point makes sense?

You can try going through the function for some case and try to figure out if what is happening is indeed what should happen.

2 Likes

Oooohhh, yes, the <= is problematic… But replacing it with only < does not change anything, I still get only the last 3 tests completed.

Thank you nonetheless!

1 Like

Alright, I will give it some more thought. I only once looked at hints thus far, at the 56th or 57th challenge, did all else by thinking it through for hours spread across days (I am not in a hurry). So yes, I will definitely think some more, I only started this.

1 Like

Changing it as recommended above does not change the results in this case. Althougth, arrays in js use zero based indexing meaning that if the index were to become equal to the array length, attempting to access this index could cause a crash. In fact, when I run my code for this challenge (which also utilizes a for loop) with a <= for the limit expression, an Uncaught TypeError results (in my IDE) when index = 4 is used to access the non-existent element.

1 Like

What I really find odious with my code is, is if I just use:

if (name === contacts[i].firstName && contacts[i].hasOwnProperty(prop) === true) {
      return contacts[i][prop];
}

within my for loop, and then run the tests, it marks as correct the first three tests. Obviously the last 3 are marked as incorrect.

And yet when I remove the above piece of code (in my for loop) and just write this instead:

if (name === contacts[i].firstName && contacts[i].hasOwnProperty(prop) === false) {
      return "No such property";
} 
    
if (name !== contacts[i].firstName) {
      return "No such contact";
}

it will mark the last 3 of tests as correct and the first 3 as incorrect (obviously).

And yet for some reason when I simply combine the two pieces of code together in my for loop it STILL will not mark the first three tests as correct, even though the first piece should execute, well, first and I am thinking it should therefore work… But it does not.

I suppose this is what is bothering me, why individual pieces of code work as intended when not used together, but when used together 1 half of it fails to execute properly.

So I have it pinpointed to this issue, the third piece of code (“No such contact”), which one of the posters mentioned as tenuous:

This:

if (name !== contacts[i].firstName) {
      return "No such contact";
}

is apparently unnecessary AND yes I understand why, since just putting this:

return "No such contact";

outside my for loop, but inside the function obviously, will make all the tests pass. I understand this perfectly.

What I still do not really understand, notwithstanding the fact that it is unnecessary, is why the third piece of code cannot stand as it is in my for loop? It makes the relevant tests pass on their own, only the tests which are not directly affected (the first three) for some reason fail and I am also failing to understand as to why …

Of course I will still think more about this, but if anyone can offer a bit detailed explanation I would love it.

Which one is the third piece of code? Could you share the full code of version, about which you have doubts?

The full version:

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

By “pieces” I just mean 3 if statements. When I just have the 1st if statement, the 3 first test results end up correct, but the 3 last ones do not. When I just have both the 2nd and 3rd if statement (but not the 1st), then the last 3 results end up correct, but the first 3 results do not.

So in isolation, both the 2nd and 3rd if statement on the one side, and 1st if statement on the other side, will result in either the last 3 or the first 3 results to be marked as correct. But when I take all the if statements together … then only the last 3 are correct.

Edit: I am aware I can make a nested if statement, but I deliberately made 3 so as to be able to see what is going on and why.

Try going through the function step-by-step for failing test.

For example, for lookUpProfile("Kristian", "lastName")

  1. In function name is "Kristian" and prop “lastName”.
  2. At the start of loop i == 0, so the contact[i] will refer to
{
    firstName: "Akira",
    lastName: "Laine",
    number: "0543236543",
    likes: ["Pizza", "Coding", "Brownie Points"],
}
  1. First if evaluates to false, as contacts[i].firstName !== 'Kristian'.
  2. And so on, further if statements and further loop iterations, until function returns. Look at what happens at each step and whether that’s what should happen.
2 Likes

Hmmm, you are correct. I think I understand what you are saying. I should have checked the examples myself, to see what will happen. I can see now why “No such contact” would be returned EVEN when there is, in fact, such a contact, precisely due to my 3rd if statement.

I feel stupid now for not checking this out first myself.

Thank you for your help, I am closer now to understanding this.

1 Like

Thank you. I have finally understood all that there was regarding this assignment, I will proceed further now.

3 Likes

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