JSFiddle - React, Tailwind, and code Playground
by Brock Whittaker
JavaScript
function stringMatch(i, a) {
counter = 0;
for (x = 0; x < a.length; x++) {
if (i.charAt(x) === a.charAt(x)) {
counter += 10; // reward 10 points for a full match
} else {
if (i.charAt(x) === 'c' && a.charAt(x) === 'k' || i.charAt(x) === 'k' && a.charAt(x) === 'c' || i.charAt(x) === 's' && a.charAt(x) === 'c' || i.charAt(x) === 'c' && a.charAt(x) === 's' || i.charAt(x) === 'g' && a.charAt(x) === 'j' || i.charAt(x) === 'j' && a.charAt(x) === 'g' || i.charAt(x) === 's' && a.charAt(x) === 'z' || i.charAt(x) === 'z' && a.charAt(x) === 's') {
counter += 7; // reward 7 points for a commonly mispelled letter
} else {
if (i.charAt(x - 1) === a.charAt(x) || i.charAt(x + 1) === a.charAt(x)) {
counter += 6; // reward 5 points for the letter being off one place.
// this is important because if they add an additional letter you don't want none of the correct lettters to be placed.
// eg. (additionally vs aditionally)
} else {
if (i.charAt(x - 2) === a.charAt(x) || i.charAt(x + 2) === a.charAt(x)) {
counter += 2;
}
}
}
}
}
counter -= Math.abs(a.length - i.length) * 3;
return counter / a.length;
}
document.write(stringMatch("apple", "aaplez"));