Last active
April 30, 2016 03:47
-
-
Save oscarmorrison/6e13367f643ef018a2a7d78438f07366 to your computer and use it in GitHub Desktop.
String Match. Match two strings with a tolerence
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 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