Basic JavaScript - Using Objects for Lookups

Tell us what’s happening:
i dont know how to make a response to a blank parameter(phoneticLookup(“”))
which should have undefined

Your code so far

// Setup
function phoneticLookup(val) {
  let 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;
}

phoneticLookup("charlie");

Your browser information:

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

Challenge: Basic JavaScript - Using Objects for Lookups

Link to the challenge:

So, if val equals an empty string you want to return undefined?

yes return undefined(but not the string(“undefined”)

if val equals “” return undefined?

youre not allowed to use if switch

Maybe you can add it to the object?

what should i assign its value?

try something, try the simplest most obvious thing and see if it works

Is there a reason you are using += here and not just = ?

because I should return result as default

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

phoneticLookup(“charlie”);

Ok, you are returning result, that’s fine, and you already define result as "" at the top. Why do you use += instead of = to assign the lookup value to the result?

I would suggest always putting these last lines in a console.log() so you can can do quick tests and always see your output. It helps a lot with troubleshooting.

Put another way, why are you adding the new value to the previous value of result, instead of just assigning it a new value?

thank you it worked but dont know how it worked and how it did nt worked before

1 Like

can I ask if you know what the answer to

“” + “”

because I think that was the error

If a key does not exist in an object, the then you will get undefined when you try to retrieve or assign from it. Now looking at this code, if phoneticLookup ("") is called, then it will try to assign ""+undefined.

Open your browser’s console and see the result of that. Even I did not expect that result, seeing that undefined and null are special :sweat_smile:

What is happening here is Type Conversion

It was strange, in console.log “”+undefined still output “undefined” but it failed the test. So it converted the special type undefined into a string?

Yes, undefined was converted into "undefined" since it was added to a String object. When one operand of the + operator is of type String, the other operand is converted to a string as well through type conversion (try it by entering 1+2+3+""+6 into your console.

This is why, to return undefined when phoneticLookup("") is called, += should not be used. result += lookup[val] is the same as doing result = result + lookup[val] which is the same as result = "" + undefined.

So use = instead.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.