This is pretty basic...
I'm stuck on what to do though.
alert("The capital of " + n + " is " + capitals.n);
capitals.n in the alert comes out as undefined. What can I do to fix that?
This is pretty basic...
I'm stuck on what to do though.
alert("The capital of " + n + " is " + capitals.n);
capitals.n in the alert comes out as undefined. What can I do to fix that?
Use square brackets:
alert("The capital of " + n + " is " + capitals[n]);
What you currently have will look for a property of capitals
with the identifier n
, which doesn't exist. Instead, you want to use the value of n
as the identifier.
Use square brackets instead of dot notation:
alert("The capital of " + n + " is " + capitals[n]);
Explanation:
capitals.n
looks for a property literally named 'n'.capitals[n]
looks for a property with the value of the variable n
as a name.(Verify by giving capitals.n
a value in your code, like: capitals.n = 'FOO'
)