Profile Lookup [my own code and questions regarding the approach]

Profile Lookup [my own code and questions regarding the approach]
0

#1

function lookUpProfile(firstName, prop){
// Only change code below this line
var o;
var j;
for(o=0;o<contacts.length;o++){
if(contacts[o].firstName==firstName){
if(contacts[o].hasOwnProperty(prop)){
for(j=0;j<contacts[j].length;j++){
return contacts[j].prop;
}
}
else{
return “No such property”;
}
}

else{
return “No such contact”;
}
}

// Only change code above this line
}

I have tryed putting 2 loops since this is a 2-dimensional array, but I thought about putting a 3rd for loop since this might be a 3 dimensional array. Am I approaching this wrong?


#2

You only need one loop. The elements inside the first array aren’t arrays; they’re functions. That means you can access their property with dot notation or bracket notation, and if that property doesn’t exist, you automatically get false (hint hint).

Also, “no such contact” is your last resort and should exist outside of any loop.


#3

I narrowed it down to 1 loop, and made it return a 2 dimensional array, but it only loops for the 1st item of the array. I don’t understand why it returns the correct answer but not makes it correct.


#4

The challenge is asking for the property’s value so the answer should be exactly as you see it written on the challenge.

So if the prop is likes it should just return the contents of likes unchanged, which in this case would be a single dimension array.

The only reason you need a loop should be to find the index that matches firstName. After that, it just needs to access right property while using the same index. The property’s content is automatically returned.

For example, once you’ve found contacts[index][firstName], the answer is contacts[index][prop].