Profile Lookup - struggling with last test

Hi,

I’ve been struggling with figuring out why my solution isn’t clearing all the tests. It won’t pass the final test. Even though when I look at my console log, it is testing as “No such property.” Any help would be appreciated.

Thanks!

function lookUpProfile(name, prop){
// Only change code below this line
var checkName = true;
var checkProp = true;

    for (var i = 0; i < contacts.length; i++) {
        if ((name === contacts[i]["firstName"]) && (contacts[i].hasOwnProperty(prop))) {
            console.log(contacts[i][prop])
            return contacts[i][prop];
        } else if (name !== contacts[i]["firstName"]) {
            console.log("No such contact");
            checkName = false;
        } else if (!contacts[i].hasOwnProperty(prop)) {
            console.log("No such property");
            checkProp = false;
        }
    }

    if (!checkName) {
        return "No such contact";
    }
    if (!checkProp) {
        return "No such property";
    }

// Only change code above this line
}

lookUpProfile("Akira", "address");

Each time you run lookUpProfile, you’re looping through the contacts array.

Since you don’t return anything in the 2 else if statements, the loop will continue until something is returned. So potentially, the checkName or checkProp will get overwritten before you even reference them later on.

Thanks for taking a look. I ended up going in a bit of a different route. I started understanding that, like you mentioned, I was just resetting the value every time it was looping through.

spoiler

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))) {
console.log(contacts[i][prop]);
return contacts[i][prop];
} else if ((name === contacts[i][“firstName”]) && (!contacts[i].hasOwnProperty(prop))) {
console.log(“No such property”);
return “No such property”;
}
}
console.log(“No such contact”);
return “No such contact”;
}

Everything now checks and makes sense. :slight_smile: Finally!! :slight_smile: :laughing:

1 Like