Please help me,I don't understand why my code is not working

Tell us what’s happening:

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 x = 0; x < contacts.length;x++){
if (contacts[x].firstName === name){
    if(contacts[x].hasOwnProperty(prop)){
        return contacts[x][prop]
    } else {
        return "No such property"
    }
}  else {
    return "No such contact"
}
}
// 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/84.0.4147.105 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

Hi and welcome to the forum!

I would think carefully about when you want to return this phrase. Have you checked all names at this point in your code?

1 Like

You forgot to tell us what’s happening. If you can describe the problem you’re having, we’re happy to help you figure it out.

1 Like

Yes,and when console.log the latter arrays in contacts,it does not read them,that is the function only reads the array containing Akira,but does not the ones of Harry,Sherlock and Kristian.

When I console.log the latter arrays in contacts,it does not read them,that is the function only reads the array containing Akira,but does not the ones of Harry,Sherlock and Kristian.

Exactly. So, what does that if statement do, in words?

What would make it stop after a single loop iteration?

Oh so basically it is not looping?

oh so basically it is not looping?How can I loop it?

your code is correct. Only thing is you didn’t print(console .log) the output.

No, the code is not correct.

@harshalc, your if statement that is checking the name is the problem. The question is why.

Any time your code encounters a return execution stops. You code, as, written, will always stop execution on the first loop iteration. This isn’t quite what you want, right?

3 Likes

@JeremyLT Oh now I understand the problem,so I have to place the last return statement out of the for loop ,correct?

1 Like

Thanks a lot,it helped

You got it! Nice work!