Profile Lookup Exercise - Basic Javascript

Profile Lookup Exercise - Basic Javascript
0

#9

I am not going to give you the answer, but I can point you in the right direction. When on Python Tutor change the values to “Sherlock”,“Likes” and see what happens. The program is returning to early.

What’s happening now is ------> because “i” is still at zero and does not find a name Sherlock in the zero index of the contact array it returns “No such contact”.
But the name "Sherlock does exist it’s just on a different index.

How would you prevent it from returning to early?


#10

Ok so help me understand something. I wrote a simple code to test this:

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

for (var i = 0; i < contacts.length; i++) {

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

else {
  
  return "No points";
}

}


// Only change code above this line
}

Now this code should return “contacts[i][prop]” but when i test it it returns “No points”. Why?


#11

sample = [ A, B, C, D]

Consider this, compare this with your code, in the sample each alphabet has all the key values as in the challenge example,

Now let’s go into your code,

Let’s say alphabet D ( at index 3 ) has both name and prop as true, so when the loop goes to D, it will output both name and prop value

And the parameter passed to the function are the values of D,

But when the loop starts at index 0, it doesn’t match any requirements for D, therefore the loop exits with the output as “No points”, but the expected output is the value of name and prop of D

That’s why your test outputs as “No points”


#12

But in the real case, the first parameters are “Akira”, “address”
which are present at index 0. So in the first loop it should be true?


#13

It should be === in your if condition


#14

why can’t it be ==. Because even if if converts to string we don’t really care.


#15

I tried with === this time with this code:

for (var i = 0; i < contacts.length; i++) {

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

else {
  
  return "No points";
}

}

it still returns “No points”


#16

Yep, i’m just little snoozy, hasOwnProperty() takes a prop as argument, but you have an array there, but in your first post, you have it correct and when i run it, its working fine for the elements that don’t match, but it doesn’t work for inputs that match


#17

Ok, i removed the [] from it. it still returns the same.


#18

prop is a string and contacts[i].hasOwnProperty(prop) returns a boolean value of true or false, so they will not be equal. Unless prop is “true” or “false” string values, the comparison will evaluate to false.


#19

contacts[i].hasOwnProperty(prop)

This returns a boolean value, but you have compared it with prop, check whether it is true, you will get the result which you expect now


#20

ok i’ll try now and let you know.


#21

Ok now it works. Thanks a lot. It got really confusing there.
I need to understand one more thing. Why is the return “No such contact” outside the for loop? Is there any other way to do this?


#22

Only once you have made it through the entire contacts array (without a return statement executing) can you know for sure the contact does not exist.


#23

Alright. Thanks for clarifying.


#24

Just one more thing. Could I have done it without using Nested if?
If i could go on with my style of code and evaluate the two statements in one if? Will i get the desired output?


#25

I do not see that you are currently using a nested if. If you have code with a nested if, you will need to post it first, so I can answer your question.

Currently, your code has a single if statement with two conditions. A nested if would look like:

if (some condition) {
  if (another condition) {

  }
}


#26

Here’s my new code. I am almost there. Every check is passed except for one:

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) !== true) {
    return "No such property";
}


}

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

// Change these values to test your function
lookUpProfile("Akira", "likes");

#27

I am getting this error: “Bob”, “potato” should return “No such contact”


#28

That is correct, because your first if statement evaluates to false, so your else if statement is evaluated next. Since prop is “potato” and the first contact (contacts[0]) does not have a “potato” property, you return “No such property” and the function exits.

EDIT: I would suggest a nested if like the one I showed in a previous reply. The outer if would make sure the firstName property matches name. The inner if would check if the property exists for that object.