How to check if two equal-length strings are at least 50% equal in Node.js

1 Answer

0 votes
function are50PercentEqual(str1, str2) {
    if (str1 === null || str2 === null || str1.length !== str2.length) {
        return false;
    }
 
    let matchingChars = 0;
 
    for (let i = 0; i < str1.length; i++) {
        if (str1[i] === str2[i]) {
            matchingChars++;
        }
    }

    return (matchingChars / str1.length) >= 0.5;
}
 
const str1 = "node.js c# c++ c python";
const str2 = "node.js c# r d rust sql";

if (are50PercentEqual(str1, str2)) {
    console.log("yes");
} else {
    console.log("no");
}
 
 
 
/*
run:
 
yes
 
*/

 



answered May 10, 2024 by avibootz

Related questions

...