Help for Using Objects for Lookups

Help for Using Objects for Lookups
0

#1

Hello. Can someone help me understand this program. Why do I need the: var result = “”; line? If I take this line out the program still works. How does the line: result = lookup[val]; work? What does it do?

Spoiler
// 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("alpha");

Link to the challenge:


#2

It is not needed. It is just some default code to give result some value. The result = lookup[val]; line assigns a different value near the end of the function, so it does not matter what was originally assigned anyway.

The variable lookup is an object. Objects have property/value pairs as explained in a previous challenge which you should review because you are asking this question. The challenge makes use of bracket notation to access the value of a specific property. Again, you should review Accessing Objects Properties with Bracket Notation based on the fact you asked this question.


#3

Please don’t post valid solutions in plain text to the forum, as it may spoil solutions for those trying to work them out for themselves.

var result = ""; declares the result variable and sets it to an empty string.

Your code may work without it, but what it actually does when it is undeclared is implicitly declare it on the fly in the global scope. Implicit global variables can catch you out or behave in ways you didn’t intend, so you should always declare your variables within the scope you are intending to use them.

You can also write it as var result; without setting it to an empty string, since the type doesn’t matter to ES5 JavaScript.

result = lookup[val] sets the value of the variable result to the value of the appropriate key in the object.

Each of the pairs in the lookup object represent a key : value pair. lookup[val] could also be written lookup['alpha'] (if the val was ‘alpha’). You can get the same value using dot notation instead of bracket notation: lookup.alpha - both of these will return "Adams".

Does that make sense?

Edit Damnit…@RandellDawson types faster than me :slight_smile:


#4

Thank you for you help.:grinning: I have some more questions.

Does ‘val’ = the appropriate pair (e.g. “alpha”: “Adams”) or does it just = the second half of the appropriate pair (e.g. “Adams”)?
Is the key of an object just the first part of a pair (e.g. “alpha”) or the whole pair (e.g. “alpha”: “Adams”)?


#5

Yes, the key is the first of the pair, and the value is the second. Val just represents the value.


#6

What is the purpose of putting ‘val’ in: function phoneticLookup(val)?


#7

val is the parameter used to pass a value into the function. Inside the function val is an argument and is treated as a variable which is then used within your function.


#8

Since ‘val’ is put in the function phoneticLookup(val), the parameter ‘val’ is capable of being used in the function. If ‘val’ wasn’t put in the function phoneticLookup(val) then it wouldn’t be capable of being used in the function. Is this correct?


#9

Yes. Think of val as a placeholder used to pass in a value to the function which will be used by the function.


#10

Would ‘alpha’ be the key in the lookup object and “Adams” the value in the lookup object?


#11

Yes, that’s correct.