JS: Using Objects for Lookups - Women Who Code.
52162
post-template-default,single,single-post,postid-52162,single-format-standard,qode-core-1.0.1,ajax_fade,page_not_loaded,,capri child-child-ver-1.0.0,capri-ver-1.6, vertical_menu_with_scroll,smooth_scroll,grid_1200,blog_installed,wpb-js-composer js-comp-ver-4.12.1,vc_responsive
 

JS: Using Objects for Lookups

JS: Using Objects for Lookups

Hello friends, I’m getting back to learning how to code after a 6 month break. I’m not going to have a lot of time, but will try to post new things I’m learning about as often as possible. Currently I’m learning JS on Freecodecamp, starting from the very basics.

Today I got stuck with this task:
Basic JavaScript: Using Objects for Lookups

There were 3 confusing things about it:
1) The 2nd sentence of instructions:
«Convert the switch statement into an object called lookup. Use it to look up val and assign the associated string to the result variable.»
2) The dot notation didn’t work for this task
3) An unexpected jump without any notice from a simple switch statement to a function with a switch statement inside and nuances of using different notations with variables.

First, I successfully converted the switch statement, but then I got pazzled about how to use the lookup object to lookup the val (function’s param) to get the result.

The final working version of code:

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

  // Only change code below this line
  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");

Since dot notation doesn’t work for this case, we use the bracket one here.
But why isn’t the dot notation applicable in this case?
Beacuse val doesn’t exist in the lookup object, it’s a function param, a variable.
This says: «take the function’s param (val) and find the matching property in the object»:
result = lookup[val];

Then we pass the string param «charlie» to the val.
But the way JS unpacks it doesn’t make sense in JS:
result = lookup.»charlie»;
Even if you created a variable called charlie and assigned the string «charlie» to it, it will end up being lookup.»charlie».
Which, again, doesn’t work.

The most useful lesson here is:

You can’t use dot notation with variables or numbers.

And a couple of accompanying lessons:

  • In a JS object, all property keys are strings.
  • Dot notation only lets you access the explicit key name of a property.
  • JS evaluates the first complete expression with square brackets in a statement, runs toString() on it to convert it into a string and then uses that value for the next bracket expression, on down the line till it runs out of bracket expressions.

Useful links:

Using Objects for Lookups
objects-for-lookups.js
JS dot-notation vs. bracket notation
r/FreeCodeCamp

No Comments

Post a Comment