function lookUpProfile(name, prop) {
for (let x = 0; x < contacts.length; x++) {
if (contacts[x].firstName === name) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
}
Code Explanation
The for loop runs, starting at the first object in the contacts list.
If the firstName parameter passed into the function matches the value of the "firstName" key in the first object, the if statement passes.
Then, we use .hasOwnProperty() method (checks if there’s a given property and returns a boolean) with prop as an argument. If it’s true, the value of prop is returned.
If the second if statement fails, No such property is returned.
If the first if statement fails, the for loop continues on to the next object in the contacts list.
If the firstName parameter isn’t matched by the final contacts object, the for loop exits and No such contact is returned.
Example Run
lookUpProfile("Akira","likes"); runs.
"Akira" is matched to the "firstName" key in the first object, so the if statement returns true.
"likes" is found within the first object, so the second if statement returns true.
The value of "likes" is returned - "Pizza", "Coding", "Brownie Points".
Solution 2 (Click to Show/Hide)
function lookUpProfile(name, prop) {
for (let i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name) {
if (prop in contacts[i]) {
return contacts[i][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
}
Code Explanation
This works as the last example but uses the in operator to look for prop instead of the hasOwnProperty() method.
function lookUpProfile(firstName, prop) {
var result = contacts.filter(x => x.firstName == firstName)
if (result.length === 0) {
return "No such contact";
} else {
return result[0][prop] ? result[0][prop] : "No such property";
}
}
or
function lookUpProfile(firstName, prop) {
let contact = contacts.find((x) => x.firstName === firstName)
return contact ? contact.hasOwnProperty(prop) ? contact[prop] : "No such property" : "No such contact”;
}
Man, I really feel like an idiot. I spent a couple of hours trying to figure this out, just to find that one of my return statements was out of place. Here my original code:
for (i = 0; i < contacts.length; i++) {
if (firstName === contacts[i].firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return “No such property”;
}
} else {
return “No such contact”;
}
}
So, I just learned the hard way that I need to loop through each index before returning anything.
I’m confused in the return line here : return contacts[i][prop];
why it doesn’t work when i replace the brackets notation with the dot. linking, since both ways are valid.
I mean like this return contacts[i].prop;
This is what I used, but I was getting an error code telling me to write != as !== which does not work. I almost used the hasOwnProperty() method but I was having a hard time making that work and this worked. I feel like there might just be a bug with the editor telling me this is wrong, does anyone have any ideas why this would be happening.
for(i = 0; i < contacts.length; i++) {
if (contacts[i].firstName == firstName) {
if(contacts[i][prop] != null) {
return contacts[i][prop];
} else return “No such property”;
}
}
return “No such contact”;
We cannot called contacts[i].prop because.prop is not a property of contacts.
When we access objects in bracket notation contacts[i][prop], we can read and pass in values, such as the function arguments (example: “lastName”, “likes”, “numbers”, etc.)
function lookUpProfile(firstName, prop)
contacts[i][prop] can pass in prop argument: lookUpProfile("Kristian", "lastName");
var property = contacts[i]["lastName"]; property = "Vos";
Hi guys, It took me the whole day and I think I am fully idiot for now because it still doesn’t work will. Here is the Code:
function lookUpProfile(first, prop){
var result = “”;
var contactTest = “firstName”;
var propTest1 = “lastName”;
var propTest2 = “number”;
var propTest3 = “likes”;
for (var i=0; i<= contacts.length; i++){
var testName = contacts[i][contactTest];
if (first === testName){
switch (prop){
case “lastName”:
result = contacts[i][propTest1];
break;
case “number”:
result = contacts[i][propTest2];
break;
case “likes” :
result = contacts[i][propTest3];
break;
default:
result = “No Such Property”;
}
}else {
result = “No Such Contact”;
}
}
return result ;
}
lookUpProfile(“Harry”, “number”);
/* main concept
if firstname exsits >>> check for property >>> if it exsiting … return its value
if Not … return no such property
if firstname isn’t exsiting >>> no such contact
*/
it gives me an error of (( Can’t read property ‘firstName’ of undefined )) !!
what’s wrong now ?!
It took me like 5 hours to understand that i had to put the return " No such contact" out of my loop
^^.
for (var i = 0; i < contacts.length; i++)
{
if (firstName == contacts[i].firstName && contacts[i].hasOwnProperty(prop))
{
return contacts[i][prop] ;
}
if (!(contacts[i].hasOwnProperty(prop)))
{
return "No such property";
}
Hi…So awesome to see everyone’s creativity.
It took me two days so I am satisfied.
Please share your thoughts on the code…
function lookUpProfile(firstName, prop){
var value;
for (var i = 0; i < contacts.length; i++){
if((contacts[i].firstName === firstName) &&
(contacts[i].hasOwnProperty(prop))){
value = contacts[i][prop];
break;
}
if (contacts[i].hasOwnProperty(firstName) === false){
value = "No such contact";
}
if (contacts[i].hasOwnProperty(prop) === false) {
value = "No such property";
}
}
return value;
}
First time poster! One question, going off the basic code solution is their an alternative way to check if the prop argument matches the property of the object besides using "
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
} else {
return “No such property”;
" .
I can’t wrap my head around why this other alternative method would not work.
if(contacts[i][prop]===prop){
return contacts[i][prop];
}
else{
return “No such property”;
}
contacts is an array of objects. So .length is only for the properties of an array
likes is also an array as well
.hasOwnProperty is a property of an object.
Also for original solution
return contacts[x][prop];
am not sure why this doesn’t work
return contacts[x].prop;
Using
return contacts[x].likes;
returns this
I think the program cannot tell whether its a variable that its being passed or an actual property of the object using dot notation
stackoverflow says this is correct
To understand the context of this, you can convert firstName into bracket notation which requires a quotation marks (from original solution)
following code works
function lookUpProfile(firstName, prop){
// Only change code below this line
for (var x =0; x<contacts.length; x++){
if (contacts[x]["firstName"] === firstName){
if (contacts[x].hasOwnProperty(prop)){
return contacts[x][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");
In summary, you should use bracket notation
when property name is contained in a variable, in this case prop, but not firstName (the variable being passed to function, the property of the object “firstName” is ok though) → this is confusing how the program named it, these should’ve been different names.
Property name contains characters not permitted in identifiers (Space, - ↑ unicode symbols, etc)
Alternative solution without using hasOwnProperty but rather a for ....in loop
function lookUpProfile(firstName, prop){
// Only change code below this line
for (var x =0; x<contacts.length; x++){
if (contacts[x].firstName === firstName){
for (var keys in contacts[x]){
if (keys === prop){
return contacts[x][prop];
}
}
return "No such property";
}
}
return "No such contact";
}