Skip to content

Instantly share code, notes, and snippets.

@StoneyEagle
Last active April 4, 2023 23:19
Show Gist options
  • Save StoneyEagle/28284218a6dc469e27c7cf55370de89f to your computer and use it in GitHub Desktop.
Save StoneyEagle/28284218a6dc469e27c7cf55370de89f to your computer and use it in GitHub Desktop.
Match percentage
const matchPercentage = (strA, strB) => {
let result = 0;
for (let i = strA.length; (i -= 1);) {
if (typeof strB[i] == 'undefined' || strA[i] == strB[i]) {
//
} else if (strA[i].toLowerCase() == strB[i].toLowerCase()) {
result += 1;
} else {
result += 4;
}
}
return (
100
- ((result + 4 * Math.abs(strA.length - strB.length))
/ (2 * (strA.length + strB.length)))
* 100
);
};
searchMovie(title.title ?? title.name, title.year)
.then((movies) => {
let show = movies[0];
let match = 0;
if (movies.length > 1) {
for (const movie of movies) {
if (matchPercentage(title.title ?? title.name, movie.title) > match) {
match = matchPercentage(title.title ?? title.name, movie.title);
show = movie;
}
}
}
return show;
});
matchPercentage('F9', 'F9') // 100
matchPercentage('F9', 'F9: All In') // -33.333333333333314
matchPercentage('F9', '機動戦士ガンダムF91') // -53.84615384615387
// returns F9: All In
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment