Free Code Camp at its best: "Using Objects for Lookups"

Example Give:

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

var value = 2;
alpha[value]; // "Y"


Answer: Doesnt even match the example… so again had to copy and past answer from hints

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

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

What is your question again ? Could you please clarify

How was I supposed to know that

return lookup[val];

was supposed to go there if the example did not clarify that I needed to add something else?

There are 2 ways to access the properties of an object , (1) by dot, . and (2) by bracket notation , [ ] . however if you have a property name that has either special characters in it, or the property name is itself a variable, then you can only use the bracket notation to access the property.
That is what the lesson is teaching you, how to access the property of an object who’s name is a variable. The variable val is passed by the function caller, in this case the string literal charlie is being passed into the function phoneticLookup , since the variable val now holds the string charlie it is being used (with the bracket notation) to access and then return the value of the property charlie of the object lookup.
FYI, you don’t need the second return statement in the function.

I am still not understanding

Ok, forget the function , here is the object only

  var lookup= {
  alpha:"Adams",
  bravo:"Boston",
  charlie: "Chicago",
  delta: "Denver",
  echo: "Easy",
  foxtrot:"Frank"
  };

If i asked you to access any of the properties of the object , do you know how to?

return lookup[alpha];

Nope, if you want to access the property alpha then you can use lookup.alpha or lookup["alpha"]
If you are not using a variable and you want to use the bracket notation then you must use quotes.

Also, you do not need the return statement as you are not returning anything, following along so far ? Let me know if it is not clear

okay but that just looks up “alpha” how do I do it so it looks up whatever variable I input.

You declare a variable and assign it a value , like so:

var myVar = "alpha"

then you use the bracket notation on myVar without quotes to access the property, like so

lookup[myVar]

which will give you the same result as our previous example, BUT, now you can change myVar assignment to anything you want and by using lookup[myVar] you can access whatever property you assigned myVar to be.

okay so how does this relate to the example.