Profile Lookup - How are first 3 tests failing?

Profile Lookup - How are first 3 tests failing?
0.0 0

#1

Tell us what’s happening:
I’m using bracket notation and proper for loops and if/else statements, yet for some reason the first 3 tests are failing, while the other 3 pass.

Does anyone have any suggestions on what may be wrong here?

Thanks!

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 (contacts[i].firstName === name) {
            if (contacts[i].hasOwnProperty(prop) === true) {
            return contacts[i][prop];
            }
            else {
                return 'No such property';
            }
        }
        else
            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; CrOS x86_64 10575.58.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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


#2

Because your for loop only runs once when i = 0. Inside the for loop, you return a value after only looking at the first contact/object in the contacts array.

Hint: Think about when/where you should be returning “No such contact”.


#3

Interesting. Thank you so much!