Using Objects for Lookups help++

Tell us what’s happening:
Hey! I can’t figure out what I’m doing wrong here. I think I’m confused by what they’re asking me to do.

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_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

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

Right now you never change result, so it’s always returning an empty string. You also declare lookup but never do anything with it.

Yeah, I think I’m not understanding what I’m supposed to do in the problem.

You’re close. You need to look for the index of “Chicago” using “charlie” result = obj[index]

Look carefully at the example code. See how they are accessing properties in an object.

I thought this might work, but I’m still totally confused


// 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 = lookup [val];
}

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

I don’t see why you have to look up Charlie though…

“Charlie” is the index of “Chicago” in the object “lookup”.

You need to assign the value of result, then return it. You can’t do it in a single line like that (at least not until you start using ES6 syntax)

result = lookup[val];
return result

Oooooh I get it now. Thank you so much for the help!!

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums