Created
August 22, 2019 03:22
-
-
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
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
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