Need Clarification: Using Objects for Lookup

Okay, so I just got finished reading every thread relevant to this challenge. I have completed the challenge. I’m just not confident I understand it.

Here is the solution to the challenge:

`` // 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"); ``

There are two parts to my confusion.
1) What is the point of var result = "";
2) What exactly is return result; returning?
I understand I am reassigning lookup[val] to var result;
but where is it getting it’s value from?

is the argument in the function call phoneticLookup();
equal to lookup[val] ?

i.e. phoneticLookup(lookup[val]);

Maybe I’m answering my own question here? The call is saying “Hey function (phoneticLookup), I need this (argument) !” and the function is saying "here you go, and it’s in this format (lookup.val or lookup[val])!

Is that right? Maybe I’m just confusing myself…

If anyone can elaborate a bit and make this idea a little more clear, I, and I’m sure other campers, would appreciate it.

1 Like
  1. The var result holds the value of the property of the object.
  2. return result is going to return the value. In this case it is going to return “Chicago”.
1 Like

So, ultimately, if the return statement was removed, the function call would do nothing?

It would have all the values but wouldn’t return anything called.

Hey Gouivel,

var result = '';

The line doesn’t actually have to be there. It’s defining and initializing the variable result to have the value of an empty string. It could be defined later in the script to read var result = lookup[val];

My thought on a potential “why” is that there is a common practice in coding of declaring all of the variables you are going to use at the top of your function. That way you can, at a glance, see what variables your function will be declaring and using and - if your naming is good - you can get a good idea of the intent and workings of the function by looking at the variables declared.

When declaring variables at the top of your function, you don’t actually have to assign a value to the variable at the time you declare it, that can come later (unless you are using const in that case you have to declare and assign on the same line).

As to what return result; is returning, it’s returning the value assigned to it, which is the value it got from the line result = lookup[value];.

Remember that, in programming, the item on the left hand side of the assignment operator ( the = sign) receives the value from the right hand side of the assignment operator. So, the line is saying “take the value stored in lookup[val] and assign it to the variable result”.

Of course, all of this is a bit long winded in that you don’t even need the variable result at all. You could simply return lookup[val]; at the end of the function.



A function in Javascript always returns something. In the case of a function without a return statement, it simply returns undefined.

1 Like

So var result = “”;

is just saying, Hey, later on you’re gonna have a variable and it’s gonna be a string. Heads up!

Kind of/sort of … but not really.

It actually is creating the variable result and it actually is assigning the value to be an empty string. It’s not doing it later, it’s doing it there.

If anything, the “hey later on” part of things is really more of a statement of intent by the programmer based on where the line is (at the top of the function) and the fact that an empty string is being assigned … which I suppose you could read to mean “I want this variable to store strings”. However, a much better way to do that would be to name the variable appropriately, something like … result_string or result_str would communicate the idea that the result is supposed to be a string.

Javascript uses dynamic variable typing, you cannot limit a variable to being only one type of value (string, number, boolean, etc.). You can always assign anything else (a Number, an Array, a Boolean) to result later in the script (unless you declared result using const, then you cannot re-assign). So naming is important (but again not a guarantee).

That’s why it the line at the top of the function could easily be var result_str;. The empty string assignment (as it stands now) has no meaning or significance other than that inferred by the programmer when reading the code.

Personally, I would use var result_str; if I wanted to declare the variable at the top of the function and then use it later. Save myself an assignment step and try for clarity in naming.

But, as I mentioned, return lookup[val]; is shortest and gets the same result (no pun intended).

1 Like

I should have clarified. What I meant was, I do understand that it IS creating the var when you declare it. But the reason it’s declared there instead of JUST inside the function is to give you a heads up on what your var is going to return when the function is called. i.e. the data type.

Actually, I think I’m just para-phrasing what you said in your first post…


Just read your last post in regards to clarity in naming. result_str makes perfect sense! Thanks.

1 Like

Hi, I solved the test myself, bat I don’t understand why firstly when I put in result += lookup[val] test did not let me pass through? Can someone explain it please.?