Proflile lookup challenge

In my code i have

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

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

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

    }

// Only change code above this line
}

on the 4th checkmark "Bob", "number" should return “No such contact”, i am getting ‘no such property’ everytime.

but when i change the code to this ( which nests the property checking condition then it works!

if (firstName === contacts[i].firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return “No such property”;
} 

so my question is this, why does the first one not work correctly and the second block with nested conditions does? Whats the difference in the outcome from the logic?? someone please explain im stuck thank you

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.

Please 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.

markdown_Forums

1 Like

In the first code, two conditions must be met for you to execute your else if

  1. (name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) is false
  2. (!contacts[i].hasOwnProperty(prop)) is true

In your second code, the No such property clause is executed when

  1. (firstName === contacts[i].firstName) is true
  2. (contacts[i].hasOwnProperty(prop)) is false

do you see the difference?

Thank you so much for the prompt reply.
Ok, here is my confusion i guess (sorry im still new to programatic thinking so proabbly why i failed here). When i see the sentence “The function should check if name is an actual contact’s firstName and the given property (prop) is a property of that contact.” I immediately think of the && in between two expressions which is why i set up the first block like that. I dont think of nesting one condition in the other like the second code block , but apparently thats the correct way to do it since it works and mines doesnt. I know that the first condtion will fail since ‘potato’ isnt a valid property, but then the program continues to the else if and thats also not true. so then shouldnt it continue the program flow and check the last condition for ‘no such contact’? or is the program done running after the last return statement which causes it to stop and not check taht last block? is that my problem?

You can do it like you said, but then you would need to change your if else a little bit.

There are 3 possible scenarios

  1. a first name matches and the property exists
  2. a first name matches and the property does not exist
  3. no first name matches

So you’d need a bit more verbose of an if else test.

But some people notice the duplication between in the first two and like to rewrite it to

There are 3 possible scenarios

  1. a first name matches and
    a) the property exists
    b) the property does not exist
  2. no first name matches
1 Like

ok so the first 3 scenarios are all possible outcomes of the logic, i follow taht so far. can you elaborate on the ‘a bit more verbose’ of the if else test? what am i missing if anything?

Your if else condition doesn’t fully describe option 2 in the first list. You are only checking for one of those two conditions in option 2.

Possible to show me that in code so that I can get a visual?

the else if statement is checking for the property even if the name doesn’t match

1 Like

We won’t write code for you but we can help you fix your code.

Like @ieahleen said, your else if is not checking if the name matches.

@JeremyLT i understand taht you will not write the code for me I apologize that wasnt my intention. So i changed my else if to have the ‘not’ property check. it works now but it seems to look very convoluted.

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)) {
            return contacts[i][prop]
        } else if (name === contacts[i].firstName && !contacts[i].hasOwnProperty(prop)) {
            return "No such property"
        }

    }
    return "No such contact"
    
// Only change code above this line
}

console.log(lookUpProfile("Kristian", "lastName"));

is that what you meant by making it more verbose?

Exactly! And now you can see that the name === contacts[i].firstName is the same between those first two, which you’ll see solutions where people rewrite the code as in your original post.

Oh! By ‘rewrite the code’ you mean that they would also initially start out with the && expressions like my conditions? And then rewrite it in the nested conditions format?

Yes, I think many people would agree that this code

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

has less repetition, and is therefore easier to parse than this code

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

The logic flow is:

  1. Find a matching contact (otherwise return “No such contact”)
  2. Assure the matching contact has the property (otherwise return “No such property”)
  3. Return the property of the matching contact

The solution with && combines steps 1 and 2 above, when logically they should flow down step by step.

1 Like

@JeremyLT When I saw the challenge sentence ‘ If name does not correspond to any contacts then return "No such contact" .’ I didnt think to create my else if condition based on that logic. I should have shouldnt i?

You can’t include that in your else if because you only know if no names match after you complete the loop over all names.

Oh i think i see what you mean now with the contacts[i].firstName being the same in both of my conditions. So that leads to the writing of the second way you said people rewrite their code with the nested format because its already expressed in the first condition. Is that what youre telling me? like the code that @colinthornton pointed out, almost like a shorthand version of my code?

Exactly. Those two numbered lists are how people tend to organize the code from this challenge.

Wow thats really smart ! I hope i can start to think/see things like that in my coding. Also, i noticed that the ‘no such property’ is only inside of the nested test for the prop and not inside of the outter test, that just popped out to me …

A post was split to a new topic: Record collection challenge question