Is there an acceptable way to handle missing fields in a template literal?
In my code example, the period after an initial appears when not desired and has extras when included.
How are initials handled (or suffixes for surnames) with variables in a template?
[code]
<!DOCTYPE html><html lang=”en”><head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width,initial-scale=1.0, user-scalable=yes”/>
<title> Test Page </title>
</head><body>
<pre id=’demo’></pre>
<script>
const write = (msg) => {
console.log(msg);
document.getElementById(‘demo’).append(msg+’n’);
}
const characters = [
{ “fname” : “Sylvester”, “MI” : “T”, “lname” : “Cat”, ‘joined’ : new Date(2020,1,1) },
{ “fname” : “Elmer”, “MI” : “”, “lname” : “Fudd”, ‘joined’ : new Date(2020,7,31) },
{ “fname” : “Tweety”, “MI” : “T.”, “lname” : “Bird”, ‘joined’ : new Date() },
{ “fname” : “Bugs”, “MI” : “B.”, “lname” : “Bunny, Esq.”, ‘joined’ : new Date() }
]
write(‘Problem below is the display of “.” when middle initial is blank’);
write(‘or double “..” when middle initial is not blank’);
write(‘n’);
for (const key of Object.keys(characters)) {
write(`${key} : ${characters[key].lname}, ${characters[key].fname} ${characters[key].MI}. tDate: ${characters[key].joined.toDateString()}`);
}
write(‘n’);
Object.entries(characters).forEach(
([key, value]) => write(`Date: ${value.joined.toDateString()} : ${value.fname} ${value.MI}. ${value.lname} t${key}`) )
</script>
</body></html>