Clarification on task

I completed the exercise, but I just wanted to check my understanding was correct (see Workings).

Using Objects for Lookups - Link

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups

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:

  1. A function called phoneticLookup is created
  2. A parameter (val) is assigned
  3. A result variable is created with an empty string
  4. A variable lookup is created.
  5. It has multiple properties thus making it an object.
  6. 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].
  7. In the console.log we call the phoneticLookup function and pass the argumen “charlie” which returns the val “Chicago”.

Thanks for your help!

It has multiple properties thus making it an object.

I mean technically you have have an empty object with no properties, but yes, this is an object.

1 Like

Hi Kevin what would you call the following? I thought these were properties?

    "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta": "Denver",
    "echo": "Easy",
    "foxtrot": "Frank"

This is also an object though: const emptyObject = {}

1 Like

Right, we’re agreeing with you that those are properties. We’re just saying that it doesn’t need properties to be an object. And technically things like arrays and functions can have properties too (because they are technically objects in JS).

I guess I misread your thing to mean that you thought it needed properties to be an object - but I guess you weren’t saying that. I’m sorry if my clarification increased the confusion.

No I understand you now. I thought an object needed properties and now I know otherwise. Thanks to you both for your tips.

1 Like