Created
January 5, 2025 05:31
-
-
Save salmanx/54713c06d5a4880a99ad5795b5958fd1 to your computer and use it in GitHub Desktop.
Format a number in a paragraph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reformatPolicyNumbers(paragraph) { | |
const policyNumbers = paragraph.split(" "); | |
policyNumbers.map((word, index) => { | |
const policyNumber = word.split("-"); | |
if (policyNumber.length === 3 && !isNaN(Number(policyNumber.join("")))) { | |
policyNumbers[index] = policyNumber.join("/"); | |
} | |
}); | |
return policyNumbers.join(" "); | |
} | |
const paragraph1 = "Your long-term policy number is 12-39-8552"; | |
const paragraph2 = | |
"Your long-term policy number is 12-39-8552 and short term policy number is 65-98-789"; | |
const paragraph3 = "Your 1st-policy-number policy number is 12-39-8552"; | |
console.log(reformatPolicyNumbers(paragraph1)); // Your long-term policy number is 12/39/8552 | |
console.log(reformatPolicyNumbers(paragraph2)); // Your long-term policy number is 12/39/8552 and short term policy number is 65/98/789 | |
console.log(reformatPolicyNumbers(paragraph3)); // Your 1st-policy-number policy number is 12/39/8552 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment