Why my code isn't working?

Tell us what’s happening:

I think I’m everything good but I don’t know why it is showing the errors. Can anyone please tell what s wrong here ?

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) {
            if (contacts[i].hasOwnProperty(prop)) {
                return contacts[i][prop];
            }
            else {
                return "No such property";
            }
        }
      
        return "No such contact";
    
    }
// 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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36.

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

What do the failing tests say?

It says “Kristian” should return “Vos”

Your code only ever checks the first member of contacts. When i is 0, it either returns a property value, returns “No such property” or returns “No such contact”.

1 Like

Do i need to write another loop to check other properties?

Can you explain how that would work?

@anasansari157 - I updated your code a bit and ran the test which fails for you.
Also wrote an alternate lookup function (alternateLookUpProfile) - less code, more readable.

<redacted>

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.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

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.

Thank you for understanding.

Ok. I have understood now that the for loop is running for once because of the last else statement. That is why my loop breaks if it doesn’t find the same firstName.
First solution comes to my mind is to remove the last statement that returns. But, ATQ, I need that too.
How to stop my loop it’s found the same firstName and tell it to continue if it hasn’t found ?

Is that the error here, @ArielLeslie ?

hint: you don’t need everything inside the loop

1 Like

Oh MY God!, I’m gonna kill myself!!.That was the only error !! That took my 4 hours !!
Hah…Finally. Thank you so much, @ieahleen

please don’t
instead be happy you were able to come up with the solution yourself!
it will become easier and easier (with some roadblocks, which instead become bigger as you go forward)

congratulations
Happy Coding