Using Objects for Lookups.. what is wrong with this?

Using Objects for Lookups.. what is wrong with this?
0

#1

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

// Change this value to test
phoneticLookup[alpha];

Your browser information:

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

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


#2

You could only use this syntax if the lookup object actually had a property named “val”. It does not so you end up returning undefined. Since val is a variable, you might want to refresh your memory on how to properly reference an object’s property with a variable. See the following challenge for how to do just that.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables/

P.S. You only need one return statement for this challenge.


#3
result= lookup[val];
or
var result=lookup[val];

I tried both yet not working!


#4

If you go with the above, make sure you still return result.


#5

yes, return results is given below the editing above line

function phoneticLookup(val) {
  var result = "";

  // Only change code below this line
  var lookup(val) = {

     "alpha":"Adams",
     "bravo":"Boston",
     "charlie":"Chicago",
     "delta":"Denver",
     "echo":"Easy",
     "foxtrot":"Frank",
  };
   result= lookup[val];
 
  // Only change code above this line
  return result;
}

// Change this value to test
phoneticLookup[alpha];

#6

I see you changed your original line of:

var lookup = {

to

var lookup(val) = {

The second will just error out, because that is not valid syntax.


#7

oh, I was just trying. but still without the (val) not working!


#8

Post full code one more time.


#9
// Setup
function phoneticLookup(val) {
  var result = "";

  // Only change code below this line
var phoneticLookup = {

     "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[alpha];

why doesn’t this one work?


#10
// 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;
}

// Change this value to test
phoneticLookup[alpha];

#11

Remove extra comma after frank


#12

still not working!

But I see your point, it shouldn’t be there.


#13

Your test call to the function is causing the code to error out. It should be phoneticLookup(“alpha”);


#14

Also delete or change the very last line to use (“alpha”) instead of [alpha]


#15

phoneticLookup(lookup.alpha)


#16

Sorry, but the variable lookup only exists inside the function, so it can not be referenced in the global scope without resulting in a Reference Error. Why? Because it is not defined in the global scope.