Created
October 14, 2016 08:06
-
-
Save CodeSigils/8cf826125f29c1619336e0ef43f57aaa to your computer and use it in GitHub Desktop.
Typo corrector
This file contains 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 typoCorrector(sourceText, target, value) { | |
// var matchPattern = target.match(/[A-Za-z]/g); | |
var matchPattern = new RegExp(target, 'g'); | |
var matchCount = (sourceText.match(matchPattern) || []).length; | |
console.log( | |
"Replacing " + matchCount + " instances of " + target + " with " + value); | |
return sourceText.replace(matchPattern, value); | |
} | |
// tests | |
function testTypoCorrector() { | |
var sourceText = 'cats dogs cats dogs'; | |
var target = 'cats'; | |
var value = 'dogs'; | |
var expected = 'dogs dogs dogs dogs'; | |
if (typoCorrector(sourceText, target, value) === expected) { | |
console.log('SUCCESS: `typoCorrector` is working'); | |
} | |
else { | |
console.log('FAILURE: `typoCorrector` is not working'); | |
} | |
} | |
testTypoCorrector(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment