Using Objects for Lookups, bug?

Tell us what’s happening:
I looked up the answer and and found the same exact code i wrote… still wont pass. Maybe a bug?

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"
  };
  // 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_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36.

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

You never change result inside the function body.
As it is, your whole function pretty much does nothing.

function phoneticLookup(val) {
 // init result as empty string
  var result = "";

  // you declare a lookup object
   var lookup = { ... };
  
  // return result, which is still an empty string
  return result;
}

Perhaps you meant to do something inside the body involving the lookup object?

Hmm, gunna risk sounding dumb here and say what am i supposed to change it too? little confused bc comment says that thats part of the setup, and to only change code in between the other 2 comments that follows. Also isn’t the whole thing part of the function body? The challenge was to turn the switch statement that it originally was into a lookup object which is what you see now.

And that’s absolutely correct. However you need to use it to actually look up at some value in it.
It’s like a vocabulary, it’s not enough to have one on your shelve, you also need to open and look up for the word, in order to see the definition :wink:

Following the example on the page, this means you can use the bracket notation to access a value inside an object

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

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

You are supposed to do something similar with your lookup object :slight_smile:


And that’s absolutely correct, but remember that you can change the value assigned to result in the function body, before the return statement

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

  // Only change code below this line
  result = "I am a totally different value now";
  // Only change code above this line
  return result;
}

That function now return "I am a totally different value now"
Hope this helps :wink:

2 Likes

Ahhhhhh i get it now, took me a while haha. Thank you!