Using Objects for Lookups - explanation

hi there,

I had a strugle with this exercise, first I was using this code:

// Setup
function phoneticLookup(val) {
var result = “”;

var lookup = {
“alpha”: “Adams”,
“bravo”: “Boston”,
“charlie”: “Chicago”,
“delta”: “Denver”,
“echo”: “Easy”,
“foxtrot”:“Frank”

};

result = lookup.val;

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

// Change this value to test
phoneticLookup(“charlie”);

and result = lookup.val; didnt pass, but as soon I changed it to result = lookup[val]; I passed…

in before exercises I was using both . notation and brackets to enter objects properties…

Can you please let me know why dot notation didnt work for this case? as Im not getting this one…

Thanks

It is because you are using a variable.
lookup.val is literally looking into lookup object for a key named “val”.
lookup[val] is looking for a key that is stored in variable val.

if you are want the literal key, it does not matter which you use, but in case u need to take the value from variable, brackets are needed.

Thank you Puukallistaja :slight_smile:

now it makes sense :wink: