freeCodeCamp Challenge Guide: Using Objects for Lookups

freeCodeCamp Challenge Guide: Using Objects for Lookups


I am Having the same issue as you -__-


I had trouble with this too, but remember earlier in the Map it said that you must use bracket notation if the property name has a space. The same thing applies if the property name is actually a string.

Think about it this way: lookup.charlie = “Chicago” but lookup.“charlie” will cause an “Identifier not found” error. Dot notation won’t work because the variable val is a actually a string and lookup.val would be interpreted as: lookup.“charlie” instead of lookup.charlie (which is what you actually want). Therefore you must use the bracket notation to pass the string variable into object query in order to access the lookup[“charlie”] value.

Interestingly, if you wanted to change the value of “alpha”, you can just type lookup.alpha = “Alberta”. The compiler knows when your identifier is actually a string, but not when the identifier is actually a string variable.

Why do you need to declare a function as a variable again within the function?

It can work with “return lookup[val];” too… no need to add return twice


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

// Only change code below this line
var lookup = {

// Only change code above this line
return result;

// Change this value to test


lookup is the variable! You are creating a new variable and then changing the value of result using your new variable!


I’m not experienced enough to know for sure but I’m thinking you don’t use dot notation to create the object literal "alpha"."Adams", It should still be created with a colon ‘;’ "alpha": "Adams" and when you use dot notation, you don’t use quotation marks, instead you type it like this: lookup.alpha and that would return "Adams". That is outside of a function, whether or not you can use dot notation to access the properties with a ‘lookup’ function, I’m not sure as I’m still practicing with the method FCC are teaching. Hopefully that helps - keep me posted as I would be interested to find out too.


Can someone please explain to me why there’s an empty variable created? var result = "" Which we later we assign lookup[val] to it.
Would it not be DRY’er to declare result at the same time we assign lookup to it?


I removed the initial result declaration but I’m not sure if I’ve done good. The code works fine though.

  var lookup = {
    "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta": "Denver",
    "echo": "Easy",
    "foxtrot": "Frank"
var result = lookup[val];
  return result;

// Change this value to test


I agree with @Guyr989. return result should not be included on the code.


SPOILER! :warning::warning::warning:

My working code here.

Pardon my code, I just commented out some of the original code in order to save time in typing :stuck_out_tongue:

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

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

      // Only change code above this line
     // return result;

    // Change this value to test


why simply return lookup.val doesn’t give results.
why do we have to define a separate variable ‘result’?


Why doesn`t result = lookup.val; work?


Recall from the challenges that we use bracket notation to look up properties in which we do not know the name for. It is very subtly implied in the second paragraph here:
Old link:


So in this case, since we do not know what the user will input for val we have to use bracket notation


I agree it is confusing. I thought I was going mad! Saying that you shoudn’t edit the code below the line slowed me down on this one! How can we suggest it should be changed?


Got it, I only remembered to use brackets only when there is a space in the name.


Could anyone help me out on this one? I think I did everything okay, but I am getting these strange errors. What am I doing wrong?


Someone help me. I understand everything that is written here very well, except for one thing. What is VAL? It doesn’t appear to be a reserved keyword in JavaScript. It looks like we are passing it into "lookup. But it is not defined, does not appear to be a variable or any type of indexed value inside the object “lookup”. If I could conceptualize this (the elephant in the room) I would easily be able to get past this excercise. In dire need of help please


Also I just want to add after reading the posts here, I was able to complete the exercise, but I still don’t understand WHY it worked. I am afraid having a poor understanding of this will give me trouble in the future, so I am going to keep thinking about this until I know it inside out!


Wait a second I may have just figured it out. Been away from code for awhile because of werk. “val” is just a placeholder and I just had a moment of autism. That’s what happened right


I’m not exactly sure why, but the code equally works by writing the name properties without them being surrounded by quotation marks, like this:

 var lookup={