Javascript Profile Lookup Help

Can somebody tell me why return contacts[x][prop]; works but return contacts[x].prop; doesn’t work?

function lookUpProfile(name, prop){

for (var 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”;

}

Your code so far


function multiplyAll(arr) {
var product = 1;
// Only change code below this line

// Only change code above this line
return product;
}

multiplyAll([[1,2],[3,4],[5,6,7]]);

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 9; SM-G9500) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36.

Challenge: Nesting For Loops

Link to the challenge:

With dot notation your code will try to access object’s property with the provided name, not through a variable with that name. In your case, it will try to access literal “prop” property that does not exist. When accessing object property “through” a variable you should use bracket notation.

Consider the situation when you have both, a variable and object’s property with the same name. You can access each of them by changing the notation:


let someObject = {
	one: 1,
	two: 2,
	three: 3,
}

let one = "two";

someObject.one // access property named "one" -> outputs '1'
someObject[one] // access property "two" through variable named "one" -> outputs '2'

1 Like