Using Objects for Lookups - What am I supposed to be learning?

Tell us what’s happening:
Describe your issue in detail here.

Could someone please explain to me in very simple language, what is it that I am supposed to be learning from this challenge?

   **Your code so far**

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

 // Only change code below this line
 
 const lookup = {
   'alpha': 'Adams',
   'bravo': 'Boston',
   'charlie': 'Chicago',
   'delta': 'Denver',
   'echo': 'Easy',
   'foxtro': 'Frank'
 };

result = lookup[val];

 // Only change code above this line
 return result;
}

console.log(phoneticLookup("alpha"));
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36

Challenge: Using Objects for Lookups

Link to the challenge:

This challenge explains one of the basic functions of a JS object, to store key/data pairs for easy reference. If you do any serious JS programming you will use objects for this type of thing all the time.

2 Likes

Hi @walidhossaini !

As a sidenote, it looks like you have a spelling error here

But to add on what was already said, it is important that you understand the basics of how to work with objects because later sections of the curriculum will dive deeper into concepts for object oriented programming.

Think of this lesson as extra practice for how to access values from an object.

2 Likes

An object is a data structure that lets you reference values by keys. In contrast, an array is a data structure that lets you reference values by numbers, or more precisely index positions.

The lookup example works like a dictionary, you look up the word (key) and get back the definition (value).

function phoneticLookup(word) {
  const phoneticAlphabet = {
    A: 'Alfa',
    B: 'Bravo',
    C: 'Charlie',
    D: 'Delta',
    E: 'Echo',
    F: 'Foxtrot',
    G: 'Golf',
    H: 'Hotel',
    I: 'India',
    J: 'Juliett',
    K: 'Kilo',
    L: 'Lima',
    M: 'Mike',
    N: 'November',
    O: 'Oscar',
    P: 'Papa',
    Q: 'Quebec',
    R: 'Romeo',
    S: 'Sierra',
    T: 'Tango',
    U: 'Uniform',
    V: 'Victor',
    W: 'Whiskey',
    X: 'X-ray',
    Y: 'Yankee',
    Z: 'Zulu',
  };

  let result = '';
  // Split the word into an array of letters
  // Loop the array and use each letter (key) to lookup the definition (value)
  word
    .split('')
    .forEach(
      (letter) => (result += phoneticAlphabet[letter.toUpperCase()] + ' ')
    );
  return result.trimEnd();
}

const word = phoneticLookup('freecodecamp');

console.log(word);
// Foxtrot Romeo Echo Echo Charlie Oscar Delta Echo Charlie Alfa Mike Papa

Consider how the code might look if you used if/else statements or a switch. Using an object can be cleaner and more efficient.

I hope that it’s okay to ask a question about this topic here.
I can’t understand why are we using var instead of let variable?

I don’t understand. Is this specific to this question? I don’t see where var is being used here. If it is about something else or a more general question, please start a new thread - we like to keep threads on one subject.