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.
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.
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
You declare a variable and assign it a value , like so:
var myVar = "alpha"
then you use the bracket notation on myVarwithout 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.