I completed the exercise, but I just wanted to check my understanding was correct (see Workings).
Using Objects for Lookups - Link
My code
// Setup
function phoneticLookup(val) {
let 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;
}
console.log(phoneticLookup("charlie"));
Workings
function phoneticLookup(val) {
let result = "";
var lookup = {
"alpha": "Adams",
"bravo": "Boston",
"charlie": "Chicago",
"delta": "Denver",
"echo": "Easy",
"foxtrot": "Frank"
};
result = lookup[val];
return result;
}
console.log(phoneticLookup("charlie"));
Looking at the code above we can see that:
- A function called phoneticLookup is created
- A parameter (val) is assigned
- A result variable is created with an empty string
- A variable lookup is created.
- It has multiple properties thus making it an object.
- Everything to the left of the colon in each property is the argument and everything to the right of the colon is the returned result or [val].
- In the console.log we call the phoneticLookup function and pass the argumen “charlie” which returns the val “Chicago”.
Thanks for your help!