Basic JavaScript - Using Objects for Lookups question on answer

Can someone explain to me how result = lookup[val] associates the I believe element of the function phoeneticLookup to the object properties in lookup? I was under the assumption brackets call up a property of the object? I assumed lookup[val] would be trying to find the property down under lookup called val.

I’m on my second round through the js course, but this really isnt making sense to me. I’m just looking for some explanation on how this works. I probably need to go over the rest another 10 times and supplement a lot of this to really get a grasp of it.

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

// Setup
function phoneticLookup(val) {
  let result = "";

  // Only change code below this line
  
  const lookup = { 
    "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta": "Denver",
    "echo": "Easy",
    "foxtrot": "Frank"
  }
  result = lookup[val]
  // Only change code above this line
  return result;
}

phoneticLookup("charlie");


The ‘val’ is an argument that is passed to the function ‘phoneticLookup’. So, the ‘val’ becomes “charlie” because we call the function with that argument. The ‘lookup’ is an object where all strings on the left side of the colon are actually parameters, and strings on the right side are the values. The ‘lookup’ variable returns the “Chicago” value for the given parameter “charlie” (it is now there because we have passed it to the function as an argument - as a ‘val’ ), and is being assigned to the variable ‘result’. The function at the end returns that value assigned to the ‘result’ variable. If you try to print output: console.log(phoneticLookup("charlie")); you will get the value of “Chicago”.

That’s making sense. I’m not sure why I couldn’t put that together. I think I was too hyper focused on the object section. Taking a break for a few days seems to have helped.

Thanks for your help!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.