Profile Lookup Exercise - Basic Javascript

What am i doing wrong with my code? I don’t want to use nested if statements to complete my task.

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 (name == contacts[i].firstName && prop == contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}

else if (name !== contacts[i].firstName) {

  return "No such contact";
}

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

}

// Only change code above this line
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

So if i store the result in a variable and return that variable in the end would it work?

Here is a great link to see what your code is doing

http://pythontutor.com/javascript.html#mode=edit

The compiler is not good. The correct program gives an error.
html%23mode%3Dedit

Did you copy and paste everything. I have 53 lines of code that I copied and pasted. from what you pasted on here.

Oh didn’t paste everything. Sorry my bad.

But can you answer my question?

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?

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?

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”

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?

It should be === in your if condition

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

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”

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

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

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

ok i’ll try now and let you know.

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?

Alright. Thanks for clarifying.