freeCodeCamp Challenge Guide: Using Objects for Lookups

freeCodeCamp Challenge Guide: Using Objects for Lookups
0

#22

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


#23

// 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(“delta”);


#24

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


#25

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.


#26

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?

SPOILER ALERT!

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
phoneticLookup("charlie");```

#27

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


#28

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";
          //break;
        bravo: "Boston",
          //result = "Boston";
          //break;
        charlie: "Chicago",
          //result = "Chicago";
          //break;
        delta: "Denver",
          //result = "Denver";
          //break;
        echo: "Easy",
          //result = "Easy";
          //break;
        foxtrot: "Frank"
          //result = "Frank";
      };
      return lookup[val];

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

    // Change this value to test
    phoneticLookup("charlie");

#29

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


#30

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


#31

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: https://www.freecodecamp.org/challenges/accessing-objects-properties-with-the-dot-operator


#32

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


#33

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?


#34

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


#35

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?


#36

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


#37

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!


#38

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


#39

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={
   alpha:"Adams",
   bravo:"Boston",
   charlie:"Chicago",
   delta:"Denver",
   echo:"Easy",
   foxtrot:"Frank"
 };