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?

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.

IAScoding1:

How does the line: result = lookup[val]; work? What does it do?

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.

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".

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.

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”)?

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?