Challenge - lookup using objects

Tell us what’s happening:
undefined is output when i use lookup.val
correct output is given when i use lookup[val]

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=lookup.val
// Only change code above this line
return result;
}

console.log(phoneticLookup(“charlie”));


// 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=lookup.val
 // Only change code above this line
 return result;
}

console.log(phoneticLookup("charlie"));

Your browser information:

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

Challenge: Using Objects for Lookups

Link to the challenge:

1 Like

Hi there,

Review the example code in the instruction again:

var alpha = {
  1:"Z",
  2:"Y",
  3:"X",
  4:"W",
  ...
  24:"C",
  25:"B",
  26:"A"
};
alpha[2]; // "Y"
alpha[24]; // "C"

var value = 2;
alpha[value]; // "Y"

To access the value in alpha, you need to use the bracket notation [] instead of .

Apply the same principle in your code.

2 Likes