Pls explain *Object lookups*

Tell us what’s happening:
var lookups = {

}
result = lookups[val]
return result

explain how it works and why it works
“Thanks for your time”

Your code so far


// Setup
function phoneticLookup(val) {
var result = "";

// Only change code below this line
var lookups ={ 
   "alpha":
     "Adams",
  
 "bravo":
    "Boston",
    
 "charlie":
   "Chicago",
    
"delta":
   "Denver",
   "echo":
    "Easy",

"foxtrot":
   "Frank"
}
result = lookups[val]
// Only change code above this line
return result;
}

phoneticLookup("charlie");

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 8.1.0; Infinix X5515 Build/O11019) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Mobile Safari/537.36.

Challenge: Using Objects for Lookups

Link to the challenge:

I will try to explain. I hope it makes sense and that i don’t mislead you.

You create a function and pass in val as a parameter. This allows you to later on enter strings within the function to determine what the function gives back.

Then in the function you define a variable called result which you then set to an empty string. This allows you to reassign the value of result later on within your function body.

You then create an object called lookups which contains key : value pairs.
To access the value within an object. You call(i’m not sure this is the right word for it) it’s key. eg:
If lookups was outside the function and i just wanted to log out Adams which is the value for the first key alpha
i would run console.log(lookups[alpha]) in the console this would give me Adams

At the end of the function this is what you do. You assign the value of result to lookups[val] but instead of passing in any of the keys within the object, you pass in the parameter val . This allows you to get any of the values in the object wherever you call the function as long as you pass in the right key.

You then return the result variable which now contains an object value

And so when you run phoneticLookup("charlie") it will return Chicago
If you were to run phoneticLookup("delta") it will return Denver

I really hope that this helps

Thanks sir it help
If I may ask how did u become so good

1 Like

So when I give a value to val
That value now goes to lookups[value of val] which is equal to results ,and results brings the property value in return
Thanks again sir

How can I be good like u

1 Like

Glad i could help.

I don’t really think i’m good, i’m a learner just like you. Just keep at it. If you don’t understand a concept read and/or watch other tutorials. Or ask for help. We’re all learning

1 Like