freeCodeCamp Challenge Guide: Using Objects for Lookups

freeCodeCamp Challenge Guide: Using Objects for Lookups
0

#13

The question I have is when you type result = lookup[val] why do you put val in square brackets? Why not put val in parenthesis like a function argument? What purpose do the square brackets have in this context?


#14

Hey guys,
I tried the bracket notation and it worked, but I tried the dot notation and it failed.
Why can’t I use the dot notation in here, what are the differences between the dot notation and the bracket notation?
Below is my code and it didn’t run well, and please help me, thank you so much!
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”);


#15

going back to the map it says “The dot operator is what you use when you know the name of the property you’re trying to access ahead of time.”

e.g.:
“property”:“value”,
"Charlie: : “chicago”,

in this case we only know the value which is “Chicago”, not the property…

I am attempting to make sense of this myself I tried keeping it as a dot and testing with the property of “Charlie” but it didn’t work.


#16

Hi DRD161, what I found helpful for your question about brackets or parenthesis (I had the same question :slight_smile: ) was an explanation on stackoverflow. It was answer number 8 on this page: https://stackoverflow.com/questions/23225375/when-to-use-parentheses-brackets-and-curly-braces-in-javascript-and-jquery Hope that helps you as well


#17

here’s the answer

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

Brief explanation:

Basically, there’s 3 ways for you to make a sort of look-up dictionary file where you give an input, and get a specific output out

  • IF / ELSE IF statements
  • SWITCH / CASE statements
  • Object array

Specifically, the object’s properties in a key-value pair.

To understand the context of this, I highlighted the keywords here in pseudocode format

  • IF (key == 1) value1, ELSEIF (key ==2) value2
  • SWITCH (key) → CASE(1) : value 1 , CASE(2) : value2
  • OBJECT ARRAY var lookup = {1:value1, 2:value2} → lookup[key]

If key = 1 in all of these examples, you get a return of value1.

Each example here does the same exact thing

Back to the original example:

This is what happens

  1. You calll the function phoneticLookup and pass the value of “charlie”
  2. The function has the object array “Lookup” inside to figure out what to output (“Chicago”)
  3. Inside the function, you store the result by searching through that object array “Lookup” by passing it an input of “Charlie” to output “Chicago”
  4. Return the value

Also, object arrays are VERY common for dictionary lookup files like this. We call this term “JSON” format, or Javascript Object Notation. Used in API calls all the time, transferring information from one app format to another, Grunt.JS files for installing NPM / nodeJS dependencies, etc

Search through a javascript open repo on github and look in the root folder for a .json file and it will follow this example


#18

This is a re-post of the full answer without altering the return line:

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


#19

I tried accessing lookup table using a dot (.) operator like this:
result = lookup.val;
However, this didn’t solved the problem but using bracket notation like below solved it:
result = lookup[val];
I have learned from FCC that both are same. So why didn’t it worked with dot operator? If both work differently, how are they different?


#20

I am Having the same issue as you -__-


#21

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?
#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