Skip to content

Instantly share code, notes, and snippets.

@jimmybatuhan
Created August 22, 2019 03:22
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 jimmybatuhan/378a79432c2860fe05a4906381c9c74e to your computer and use it in GitHub Desktop.
Save jimmybatuhan/378a79432c2860fe05a4906381c9c74e to your computer and use it in GitHub Desktop.
Compare a name to a collection of names, returns the percentage of each names
let name = 'James Bond';
let names = [
'Goerge Mallory',
'Marie Curry',
'Tom Yorke',
'Jimmy Wales',
'James Blonde',
'j@M3$ b0ND',
];
exports.match = function(name, collection){
let _name = name.trim(``).replace(` `, ``);
let matchResult = {
subject : name,
length : _name.length,
result : []
};
for(let i = 0; i < collection.length; i++){
let collectedName = collection[i].trim(` `).replace(` `, ``);
let matches = 0;
let matchPercent = 0;
matchResult.result[i] = {
name : collection[i],
length : collectedName.length,
percentInNum : 0,
percentInString : `0%`,
};
//Loop each character of the subject
for(let charIndex = 0; charIndex < _name.length; charIndex++){
if(_name[charIndex] === collectedName[charIndex]){
matches++;
}
}
matchPercent = Math.round((matches/collectedName.length) * 100);
matchResult.result[i].percentInNum = matchPercent;
matchResult.result[i].percentInString = `${matchPercent}%`;
}
return matchResult;
};
console.log(this.match(name,names));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment