freeCodeCamp Challenge Guide: Using Objects for Lookups

freeCodeCamp Challenge Guide: Using Objects for Lookups
0

#1

Objects can be thought of as a key/value storage, like a dictionary. If you have tabular data, you can use an object to “lookup” values rather than a switch statement or an if/else chain. This is most useful when you know that your input data is limited to a certain range.

Here is an example of a simple reverse alphabet lookup:

var alpha = {
  1:"Z",
  2:"Y",
  3:"X",
  4:"W",
  ...
  24:"C",
  25:"B",
  26:"A"
};
alpha[2]; // "Y"
alpha[24]; // "C"

#2

Hi everyone. I have a huge problem with this challenege. I got stuck for several hours. Whta do I do wrong?
// Setup
function phoneticLookup(val) {
var result = “”;

// Only change code below this line

var val = {
“alpha”: “Adams”,
“bravo”: “Boston”,
“charlie”: “Chicago”,
“delta”: “Denver”,
“echo”: “Easy”,
“foxtrot”: “Frank”,
};
// Only change code above this line
return result;
}

// Change this value to test
phoneticLookup(“charlie”);


#3

Hi, Michuello
Change the var Val to var lookup
add return lookup.val on the next line after defining var lookup value

e.g

// 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”
};
return lookup[val];

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

// Change this value to test
phoneticLookup(“charlie”);


#4

This answer works, but it makes the second return unreachable? Is that ok?


#5

Use:
result = lookup[val];

and keep the return…


#6

update : use result = lookup[val] instead, that works better without error like @prlndgrn said it.
i was using the original solution with error even when the second return shows unrechable and it even pases the test.
thanks @prlndgrn for the correct info.


#7

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

// Only change code below this line
switch(val) {
case “alpha”:
result = “Adams”;
break;
case “bravo”:
result = “Boston”;
break;
case “charlie”:
result = “Chicago”;
break;
case “delta”:
result = “Denver”;
break;
case “echo”:
result = “Easy”;
break;
case “foxtrot”:
result = “Frank”;
}

// Only change code above this line<-------------------------this line should not be here.
return result;<---------------------------------------- This is the return that should’ve been changed to:
return result = lookup[val];
}

As you can see, it says only to change the code above the line.
This is extremely confusing.
is it a bug? or there’s another way?


#8

Can you explain why var result = “”; is necessary? I think it is to catch the return result, but I want to make sure.


#9

your comments code is altered reset the challenge and try again


#10

ANSWER BELOW
_
_
_
_

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

Using objects for lookup - still having issues in 2018!
#12

Yep you got it right, the reason for var result = " " is to create an empty string. This is where the return result; statement actually “returns” the results. It’s like taking the returned results and placing them in a container (box, etc.) You get the results after the code runs but you need someplace to put it so the return statement takes those results and “returns” them into that “box” (which in this case would be var result = " ") Hope that makes sense.


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