Profile lookup looping problem

Try this, it should fulfil your requirement.

<redacted by moderator>

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

I don’t understand what you are saying, the function is called with many different arguments, for some that is true for the first element in the array - for example if name equals "Harry" for the first element in the array it will be contacts[0]["firstName"] !== "Harry" or "Akira" !== "Harry" which is true

Sorry for trouble, I was unaware of that, I will take care from next time.

@ilenia , I mean, when the function is being called like lookUpProfile("Akira", "likes"); this, then code inside first if statement will be executed (mentioned if statement below) and will get desired results.

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

But, when the function call is given like lookUpProfile("Harry", "likes"); then below code will be execute.

if (contacts[i]["firstName"] === name && contacts[i].hasOwnProperty(prop) === true) {
   return contacts[i][prop];
} else if (contacts[i]["firstName"] !== name) {
   return 'No such contact';
}
  1. It will check contacts[0]["firstName"] === "Harry" or "Akira" === "Harry" which is false, so if condition gets false and then control will move towards else if statement.
  2. Now it will check contacts[0]["firstName"] !== "Harry" or "Akira" !== "Harry" which is true so it will execute the code inside it.

so, @Wairua, little hint from my side -

  1. First apply condition for first name, then apply nested condition for non existence of property.
  2. If name compare fails then apply condition to check the length of an array so that it will get to know that the array element are now completed but still no such name exists in array.

@ilenia , If you don’t mind will you help me for my question? ’ Unable to get nested array of objects from json-server
I am trying to solve it from last 5 hours but no success, please help me.

I am not able to help you

Hey Wairua,

I think you choose a difficult approach to solve the issue. In programming it is often a good idea to divide a larger algorithm problem into smaller problems.

I suggest you divide your algorithm in those two parts:

(1) find the contact entry in the list that has the correct “firstName”. Save that matching contact entry to a local variable for example “matchinContact”.

If there is no contact entry that matches the required name => return “No such contact” before you continue with the next step.

(2) You found the correct contact entry in (1), the reference to the matching contact entry is saved in your local variable “matchingContact”. Now you can use the “hasOwnProperty” function or “in” operator to find out if “matchingContact” contains a property with the identifier “prop”.

If that you find such a property, return it.

If you don’t find such a property return “No such property” instead.

I hope this suggestions helps you, and keep coding!

best
Dennis

1 Like

if the argument (name) doesn’t match the firstName property then it should return the string literal ‘No such contact’

it should return "No such contact" if it doesn’t match the first contact in the array or if it doesn’t match all of them?

it is supposed to return ‘No such contact’ after it doesn’t match all of the array elements

and we go back to the first post

clarification, are you asking what will happen when i pass in ‘Akira’ as the name argument to the function?

for "Akira" that statement is false, no, something like "Harry"

‘Harry’ would be caught by the first condition in the if block (hence the if block will return the property if it exists) so the if else block won’t run

when i is 0 you have

if ("Akira" === "Harry" ... ) {
   ...
} else if ( "Akira" !== "Harry") {
   ...
} ...

so the else if statement is the one with a true condition and the one that execute

1 Like

now i see, so the loop should continue to the next element in the array after verifying that ‘Akira’ !== ‘Harry’ rather than returning ‘No such contact’. Is that it?

yes, as it is now your loop stops at first iteration

thank you for the help. Let me try rereading the for loop because i am still finding it a challenge to solve that problem.

hint: the logic doesn’t have to be all inside the loop

1 Like