return contacts[i][prop]; //This works perfectly
return contacts[i].prop; //Why this doesn't
return contacts[i].prop wont work since contacts is an array and not an object.
I write the same code with you . I think the mistake is RETURN will end the loop.
I did the same but mine didn’t work because of the last return. I have to remove the last else statement and just place return before closing the function. Not sure why it does that.
.
.
.
else {
return “No such property”;
}
}
}
return “No such contact”;
// Only change code above this line
}
had the same issue. not sure why
Hello mr. catattac,
shoudln’t it work like this? It returns false on the hasOwnProperty check, byt I don’t know why. Could you help?
function lookUpProfile(firstName, prop){
for(var i = 0; i <= contacts.length; i++){
if(contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}else if(!contacts[i].hasOwnProperty(prop)){
return “No such property”;
}else{
return “No such contact”;
}
}
}
It took me a couple hours to figure it out…
I was overthinking it, but I kept writing firstName[i] instead of contacts[i]… I know some of y’all got the same answer, but I hope this helps someone. haha
for (var i = 0; i < contacts.length; i++){
if (contacts[i].firstName === firstName){
if (contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
} else {
return “No such property”;
}
}
} return “No such contact”;
took me a couple of hours because I was overthinking so it caused me to solve it the long way lol
var contactLocation = 0;
var nameIsReal = false;
function lookUpProfile(firstName,prop){
// Only change code below this line
for(var i = 0; i < contacts.length; i++){ //iterating over array
nameIsReal = false; //resetting validation
if(contacts[i].firstName == firstName){ //checking if contact is real
nameIsReal = true;
contactLocation = i;
console.log(nameReal);
break;
}
}
if(nameIsReal != true){
return “No such contact”;
}
if(contacts[contactLocation].hasOwnProperty(prop) == true){ //checking if property is real
return contacts[contactLocation][prop];
}
else{
return “No such property”;
}
@AhmedMJ and @anon24349556, I think this is because when you use dot notation, you’re assuming that whatever comes after the dot is an actual property within the object. So, for example,
contacts[x].firstNameworks because “firstName” is actually the name of a property within contacts. However, “prop” is not actually the name of any property within contacts. Rather, it’s the name of the argument (whether it’s “lastName”, “number”, or “likes”, or something else that would return “No such property”) that you are going to pass through the lookUpProfile function. Therefore, if you type
contacts[x].propit actually doesn’t mean anything because no such property of “prop” actually exists within contacts. Typing it with brackets instead,
contacts[x][prop]signals that, just like we have defined x = 0 and so we can use x to iterate through the object, we have defined prop as an argument to be passed. So if we called the function:
lookUpPropfile(“Akira”, “likes”)contacts[x][prop] would first iterate through contacts looking for whether any of the firstName’s matches “Akira”. Then, within that object that includes the firstName of “Akira”, it would look for whether there’s any property called “likes”. If there is, it will return the value of the “likes” property.
A really long explanation, but I hope that makes sense! This is just my personal understanding. I’m not sure if it has any accurate basis.
I do think that it’s slightly confusing because they use the same name of firstName for the function argument and the contacts property. I believe it would be less confusing to name it something else, like @codeolas mentioned.
The solution given at the top assumes that firstName is unique in the array.
If firstName isn’t unique then there could be scenario where a first matched object doesn’t have the required property, but a subsequent matched object does have the required property. However, the code would already have returned “No such property” in this scenario, which is incorrect.
The example requires three checks, and order is important:
- If both are true, then return the “value” of that property.
- If firstName does not correspond to any contacts then return “No such contact”
- If prop does not correspond to any valid properties then return “No such property”
My example code:
function lookUpProfile(firstName, prop){
// Only change code below this line
var foundName = 0;
var matchProp = 0;
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName == firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else if (contacts[i].firstName == firstName) {
foundName = 1;
} else if (contacts[i].hasOwnProperty(prop)) {
matchProp = 1;
}
}
if (foundName == 0) {
return “No such contact”;
}
if (matchProp == 0) {
return “No such property”;
}
// Only change code above this line
}
This is my code. It is not so clean but maybe for someone will be useful.
function lookUpProfile(firstName, prop) {
// Only change code below this line
for (i = 0; i < contacts.length; i++) {
if ((contacts[i].firstName === firstName) && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
else if ((contacts[i].hasOwnProperty(prop)) !== true) {
return "No such property";
}
}
return "No such contact";
// Only change code above this line
}
I did it like that, is it correct ? Please answer.
function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i = 1; i < contacts.length; i++){
if(firstName == contacts[i].firstName && contacts[i][prop]) {
return contacts[i][prop];
} else if(!contacts[i][prop]){
return "No such property";
}
}
return “No such contact”;
// Only change code above this line
}
Hello, could anyone let me know why in the first if condition we aren’t matching the argument firstName to the object’s key firstName without the double quotes? i.e firstName === “firstName”
please guys…why didn’t this work

This challenge has me wondering is coding is for me. I spent two days on it, then I went back to the beginning of the js challenges and did them all over again, thinking by the time I got back to this challenge I would have learned whatever I was missing the first time. I got back here again and spent all morning on it and eventually cracked and looked at the basic code solution. I had it all until I got into the first ‘if’ statement and I couldn’t figure out to direct it to the right spot. Maybe my brain just doesn’t work for coding? I don’t think the way I need to I guess. Maybe I should just quit before I waste any more time.
I feel the same way. Everyone keeps saying just keep doing it, but I either solve the challenge in a few seconds with ease(because it’s just changing something that’s basically given to you) or give up and look at the answer because it seems impossible. There’s never a middle ground or a challenge that I feel I could solve if I just worked at it.
Hey fellow campers,you could approach this challenge like this:
//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(firstName, prop){
// Only change code below this line
var str;
for(i = 0;i < contacts.length;i++) {
//check firstName from our data
if(firstName == contacts[i].firstName) {
/*check whether our profiles
have that property or not*/
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”);
I was a little creative using a while statement, since that’s how much interpreted the solution. However, it took me a long time to figure out the placement of the last return statement. That’s where most of my confusion came from.
function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i=0;i<contacts.length;i++){//if first name
/**
* make sure the firstName param matches the object's firstName
*/
while (contacts[i].firstName==firstName) {
if(contacts[i][prop]){
return(contacts[i][prop]);
} else {
return "No such property";
}
}
}//end for loop
return "No such contact";
// Only change code above this line
}
Whoah…
I spent 2,5 hours on this and my solution worked but seems to be super inefficient and very complicated looking at the “normal” solutions. I have spent 1-2 hours more understanding the solution given by FreeCodeCamp.
Anyway, if someone finds it interesting, here is my solution. As as I said, very complicated and probably a waste of resources:
function lookUpProfile(firstName, prop) {
var checkName = "";
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === firstName) {
checkName = contacts[i].firstName;
break;
} else {
checkName = "No such contact";
}
}
var checkProp = "";
for (var j = 0; j < contacts.length; j++) {
if ((contacts[j].hasOwnProperty(prop)) === true && checkName != "No such contact") {
checkProp = contacts[i][(prop)];
break;
} else {
checkProp = "No such property";
}
}
switch (checkName) {
case "No such contact":
console.log (checkName); // I have put there return instead of console.log.
default:
console.log (checkProp); // I have put there return instead of console.log.
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Sherlock", "likes");
Figured I’d share my code and hopefully it helps someone. I nested an if statement so that if prop comes back as non-existent, an error message would be displayed.
for (var i = 0;i < contacts.length; i++) {
if (contacts[i].firstName == firstName) {
if (contacts[i][prop] == null) {
return "No such property";
}
return contacts[i][prop];
}
}
return "No such contact";