Profile lookup looping problem

Tell us what’s happening:
i have checked the hint and i understand the concept behind it. My solution was longer than the one provided. It works for the first element (object) in the array, but it doesn’t loop to the next element. Kindly help me understand where i am getting this wrong.

  **Your code so far**

// Setup
var 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 (var 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) {
          return 'No such contact';
      } else if (contacts[i].hasOwnProperty(prop) !== true) {
          return 'No such property';
      } else {
          return 'kindly enter valid details';
// 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/91.0.4472.124 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

try going through your code step by step, what happens if this is true for the first element in the array?

@Wairua, This is all happening because of your if-else conditions.

so when you call lookUpProfile("Akira", "likes"); for “Akira” it goes inside first if statement and checks for the first element and as first element name is same as that of what name you are passing in, so it executes the code inside that if and returns likes array.

Now, check for the another call lookUpProfile("Harry", "likes");, So here again on first if statement it checks, if the first name in the array ("Akira" here) matches with the name you have passed in ("Harry" here), but it fails as names doesn’t match, so then control goes to else statement and where there is an another if statement which identifies if name in the array ("Akira" here) is not equal to the name you passed in ("Harry" here), and as they are not equals control gets inside it and return ‘No Such Contact’.

Well, I am not able to understand the significance of your code but try to use map() method instead of for loop.

For first element it will never be true because it is inside else statement which doesn’t executes as its if condition will be true for the first element.

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!


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