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
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.
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
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.