What's the problem in this code

Tell us what’s happening:

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"
};
var val= "delta";
 result=lookup.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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.

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

result=lookup.val;

This code is looking for the “val” key inside the lookup object. What you want is what is inside the val variable, the only way to access an object key using a variable is using bracket notation, like so:

result=lookup[val];
1 Like

thanks for answer , but now appeared a new problem :frowning_face:

when i remove " " , this problem appears

Remove the following line:

var val = delta;

You probably put it there to debug?

1 Like

yes , now it’s working but i didn’t understand how does it work !

Try to guide the person and not only provide a solution, that way they would understand what’s going on and be able to solve it themselves…

The variable val is coming from the function argument in this line:

function phoneticLookup(val) {

Which means the test is calling it like this:

  • phoneticLookup("alpha"); // then val is alpha
  • phoneticLookup("bravo"); // then val is bravo
  • etc…

Is that what you’re not understanding?

2 Likes

yes exactly that what i want to understand , thank you very much :slight_smile: