0

So this code is meant to take an array (which is represented by different people in someone's contact list) and use a lookUp function with two arguments (a person's first name and a property, both provided by the user, to determine:

  1. whether or not the person is in fact in your contact list
  2. whether or not the property actually exists in that person's individual array
  3. If both values return true, print the value of the property provided

     function lookUp(firstName, prop){
       var i;
       var newArray = [];
    
    for (i = 0; i < contacts.length; i++) {
       var fn = contacts[i].firstName;
       newArray.push(fn);
    }
    
    var foundName = $.inArray(arguments[0], newArray) > -1;
    if (foundName === true){
       var place = $.inArray(arguments[0], newArray);
       var property = arguments[1];
          if (property == "likes"){
       return contacts[place].likes;
           } else if (property == "number"){
       return contacts[place].number;
           } else if (property == "lastName"){
       return contacts[place].lastName;
           } else {
          return "No such property";
       }
       } else {
       return "No such contact";
       }
    

And an example array for sake of ease:

       var contacts = [
       {
       "firstName": "Akira",
       "lastName": "Laine",
       "number": "0543236543",
       "likes": ["Pizza", "Coding", "Brownie Points"]
       },
       {
       "firstName": "Harry",
       "lastName": "Potter",
       "number": "0994372684",
       "likes": ["Hogwarts", "Magic", "Hagrid"]
       },

Now the function works fine as is, but in my previous version I had this to output the property of an existing contact:

       var foundName = $.inArray(arguments[0], newArray) > -1;
       if (foundName === true){
       var place = $.inArray(arguments[0], newArray);
       var property = arguments[1];
       return contacts[place].property;
       } else {
       return "No such contact";
       }

Instead of the revised "workaround" of my new code, this:

       var foundName = $.inArray(arguments[0], newArray) > -1;
       if (foundName === true){
       var place = $.inArray(arguments[0], newArray);
       var property = arguments[1];
          if (property == "likes"){
       return contacts[place].likes;
           } else if (property == "number"){
       return contacts[place].number;
           } else if (property == "lastName"){
       return contacts[place].lastName;
           } else {
          return "No such property";
       }
       } else {
       return "No such contact";
       }

With the previous version of code, even though var property is equal to "likes," nothing would be returned; contacts[place].likes would return a list of likes, but contacts[place].property would return nothing at all. Hence my workaround code, in which I just take each property one-by-one to see if it exists. Why exactly does this happen? Why can't a variable (which means literally exactly the same thing) take the place of the property in question, and achieve the same results?

lift-it-luke
  • 407
  • 1
  • 6
  • 11

2 Answers2

0

You need to access the property using bracket notation. I would write it as such...

var place = $.inArray( firstname , newArray);
if ( place > -1 ){
    return contacts[ place ][ prop ];
} 
else {
    return "No such contact";
}
etchesketch
  • 821
  • 4
  • 14
0

Short answer: grammar.

Longer answer:

In an expression like foo.bar, the token foo is the name of a variable that refers to an object. The token bar is the name of an attribute of that object. bar is not a variable name. This is the definition of the language.

Instead, in an expression like foo[bar], the token foo is, still, the name of a variable that refers to an object. The token bar, however is now also the name of a variable that refers to a value (usually a string, or integer) that is the name of an attribute.

dsh
  • 12,037
  • 3
  • 33
  • 51