Tell us what’s happening:
my code passes every test except for translating Titles such as “Mr.” to “Mr” even though it does in practice locally. It also capitalizes it just as the given FCC project does. What is wrong with the test or what am I missing?
So when I inspect the element inside Google Chrome I figured out why you were failing the test.
Basically you are leaving a blank after removing the ‘.’ from the title. Which leaves a blank space inside the span tag.
You should also remove the blank space after removing the ‘.’ to reach deep equality.
For example:
freeCodeCamp tester is asking for: '<span class="highlight">Dr</span> Grosh will see you now.'
But you are providing: '<span class="highlight">Dr </span> Grosh will see you now.'
Solved. I was using a RegEx which included a matching space, so it was adding a space in between the span for titles in American to British. Simple fix was to just remove it, I originally put the “/s” to capture all titles only being used in the middle of a sentence and to avoid ones at the end, but I guess that is a rare occasion. Thank you @ofk8vb !
New working code:
var titleRegex = /(mr|mrs|ms|mx|dr|prof)\./gi;
var titles = translation.match(titleRegex);
if (titles) {
titles.forEach((title) => {
translation = translation.replace(
title,
`<span class="highlight">${(title.charAt(0).toUpperCase() + title.slice(1)).replace(".", "")}</span>`
);
});
}