0

I can't seem to get the records i need out from the multidimensional array. And it's not giving me any particular error that can make me pinpoint what I am doing wrong.

Can you assist?

//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 i = 0;
  while(i < contacts.length) {
    if (contacts[i].firstName === firstName) {
      return contacts[i].prop;
    }
  i++;
}
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");
mplungjan
  • 169,008
  • 28
  • 173
  • 236

2 Answers2

1

Change

return contacts[i].prop;

to

return contacts[i][prop];

To expand a bit: in your original code, you're asking to get the field called "prop" from the contact. Your intent is that you want the field which has the name of the value of the variable prop.

Eric Dobbs
  • 237
  • 6
  • 12
  • What do you mean? Sorry, I'm still a bit new to answering on SO, just curious on how to improve. – Eric Dobbs Aug 04 '16 at 19:41
  • The problem is so trivial it can be closed as a typo. Your answer is correct and ok, just the problem is too simple to answer in my opinion. See my comment to the question. Leave your correct answer and perhaps it will be accepted and you get some rep. – mplungjan Aug 04 '16 at 19:42
1

Change contacts[i].prop; to contacts[i][prop];

Oleg Imanilov
  • 2,591
  • 1
  • 13
  • 26