Write a function named formatName
that takes as a parameter a JavaScript object
representing name data in the following format:
{
"first" : "FIRSTNAME",
"middle": "MIDDLENAME (optional)",
"last" : "LASTNAME"
}
and returns a string representation of the name in the format "Last, First M."
where only the first letter
in the first and last name parts is capitalized, and only the capitalized initial of the middle name is
included. If there is no "middle" name key for the passed object or if the value for the key is the empty
string, the returned string should instead be in the format "Last, First"
.
For example, for the following variable:
let fullNameData = {
"first" : "foo",
"middle": "mumble",
"last" : "bar"
}
the call formatName(fullNameData)
should return "Bar, Foo M."
.
For the following variable (without a middle name):
let partialNameData = {
"first" : "Mowgli",
"last" : "Hovik"
}
the call formatName(partialNameData)
should return "Hovik, Mowgli"
.
You may assume that the passed object is non-null and that values for "first", "middle",
and "last" will always be of the string type, but if missing either of the required
"first" or "last" keys your function should throw an exception with the message,
"Passed object must have keys of 'first' and 'last'".
If instead either "first" or "last" values are empty strings, your function should throw an exception
with the message, "Each name part must have a non-empty string value".