Skip to content

Instantly share code, notes, and snippets.

@oscarmorrison
Last active April 30, 2016 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarmorrison/6e13367f643ef018a2a7d78438f07366 to your computer and use it in GitHub Desktop.
Save oscarmorrison/6e13367f643ef018a2a7d78438f07366 to your computer and use it in GitHub Desktop.
String Match. Match two strings with a tolerence
function matchString(stringOne, stringTwo, tolerance){
stringOne = stringOne.toLowerCase().replace(/ /g,'');
stringTwo = stringTwo.toLowerCase().replace(/ /g,'');
var minLen = Math.max(stringOne.length, stringTwo.length);
var bigLen = Math.min(stringOne.length, stringTwo.length);
for(var i = 0, count = 0; i < minLen; i++){
if(stringOne[i] === stringTwo[i]){
count++;
}
}
var ratio = count/bigLen;
return ratio >= tolerance;
}
//Example
console.log(matchString("oscar", "Oskar", 0.8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment