Using Objects for Lookups

Tell us what’s happening:
Everything has passed however, i am not sure how to return undefined for phoneticLookup("")

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

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

Your browser information:

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

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

You can return undefined by

return undefined;

or

return;

right but we are not supposed to change the return line

You can assign undefined to the variable result.

result has been assigned the object with the function parameter

Yes, but at no point is result ever undefined;

I see, so how would i assign result undefined while maintaining all other output values for result?

Right now result is always a string. It starts out as an empty string and your line result += lookup[val] appends to that string. If val is not a property in lookup, then lookup[val] is undefined, but when you append it to an empty string the result is still an empty string.

Yes.
Ok, I completely understand.
Because I was appending the result variable, it was maintaining the empty string that it was declared and assigned as and when lookup[val] is undefined, the result variable would still hold the empty string that it was originally declared and assigned with.

Rather than add on to the string as I did when I appended it using += I should have originally just reassigned it the variable

Exactly :smiley:

Thank you for your help and pointing that out and explaining this to me :smiley:

I’m glad that I could help. Happy coding!