Using Objects for Lookups, Please help

Tell us what’s happening:
I was sick of spending 30 minutes on this, so I went to get a hint. Nothing different between my code and its. But I decided maybe there was a comma or something I didnt see. I deleted my code and copy/pasted the hint code, note the solution. Didnt work? I don’t know whats wrong with the code.

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");

Did you remember to change what is being returned?

1 Like

Wouldent " return result;" Given below the code allowed to change do that?

It’s weird sometimes how they write the return values. You can either change the return value (tests will pass just fine as long as it is returning what it should) or you can update what the variable result should contain prior to the return. Hope that makes sense?

So an example might look like:

// 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"
  };

  //add the following line:
  result = //put required value here to make the tests pass

  // Only change code above this line
  return result;
}

// Change this value to test
phoneticLookup("charlie");
1 Like

Well, result is still just "" and you are not doing anything with val

Only problem with that is, I need to make the var work as a if statement without using the if statement. I need to give it Adams when it states phoneticLookup(“alpha”) and so on. And I can’t “cheat it” by using switch or if. I used the code in the hint. Just returning all of it doesnt work either

You have an object now. Your work is not done, you are not doing the actual lookUp

Use the object and val to give result the desired value

No need for an if or switch statement, the problem is that you aren’t actually looking anything up.

Your return statement needs to return the variable that it needs to access and the input that needs to be accessed within: variable[input]

Thank you, I used

 result = lookup[val];

And It worked perfectly.

1 Like

Perfect! Great job. :slight_smile: