What is the relationship between var result = “” and var lookup? How does var result know to replace the string value with the properties of var lookup?
To me it looks like there’s two independent variables within the function, I don’t understand how they’re linked. I feel like there’s a gap in my knowledge which hasn’t been explained in the content so far.
Your code so far
// 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;
}
phoneticLookup("charlie");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0.
There is no relation between var result="" and var lookup{}
But we can relate them like this: result = lookup[val];
And if a function has two return statements one below the other only the 1’st return statement will execute. Second return statement will never be executed.
if you don’t use result variable we get error You should not modify the return statement Since return statement uses result you must assign the value to result. And the challenge clearly says assign the associated string to the result variable.
// Setup
function phoneticLookup(val) {
var result = ""; // Empty string is assigned to result. So result is "".
// Only change code below this line
var lookup={
"alpha": "Adams",
"bravo": "Boston",
"charlie": "Chicago",
"delta": "Denver",
"echo": "Easy",
"foxtrot": "Frank"
} // we are creating object called lookup here.
result=lookup[val]; // Accessing the object propertie using bracket notation and variable.
// Only change code above this line
return result; // We are returning the result i.e. "Chicago".
}
phoneticLookup("charlie");
We can access the properties of a Javascript object in three ways: