Using Objects for Lookups - Please, what's wrong with my code?

Using Objects for Lookups - Please, what's wrong with my code?
0

Tell us what’s happening:
Please could someone help out to identify the problem with the code I wrote? It continues to fail the test.

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"
  };
  result = phoneticlookup(val);


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

// Change this value to test
phoneticLookup("charlie");```

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups

  result = phoneticlookup(val);

phoneticlookup doesn’t exist. Assuming that you meant phoneticLookup, can you explain how you expect this line of code to work?

Your problem is in this line. First, your goal is not to call the function phoneticLookup again but access the value in the object lookup based on the provided key. In this case, your desired key is represented by val, or “charlie”.

Besides, when using variables or properties that can change (which is your case), you have to use the bracket notation to access the value assigned to the object key. How can you refactor your code to access the value inside the object key?

1 Like

“Besides, when using variables or properties that can change (which is your case), you have to use the bracket notation to access the value assigned to the object key. How can you refactor your code to access the value inside the object key?”

Hi, I actually meant phoneticLookup. Typo! Could you kindly clarify which is the object key and which is the value inside the object key?

const objectName = {
   "key": "value"
}

objectName["key"] // returns "value"

How can you use val from the phoneticLookup function to return a value from lookup?