
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Compare Strings in JavaScript and return percentage of likeliness
We are required to write a JavaScript function that can compare two strings and return the percentage likeliness of how much they are alike. The percentage will be nothing but a measure of many characters the two strings have in common.
If they are completely similar the output should be 100, and if they contain no common character at all, the output should be 0.
Example
const calculateSimilarity = (str1 = '', str2 = '') => { let longer = str1; let shorter = str2; if (str1.length < str2.length) { longer = str2; shorter = str1; } let longerLength = longer.length; if (longerLength == 0) { return 1.0; } return +((longerLength - matchDestructively(longer, shorter)) / parseFloat(longerLength) * 100).toFixed(2); }; const matchDestructively = (str1 = '', str2 = '') => { str1 = str1.toLowerCase(); str2 = str2.toLowerCase(); let arr = new Array(); for (let i = 0; i <= str1.length; i++) { let lastValue = i; for (let j = 0; j <= str2.length; j++) { if (i == 0){ arr[j] = j; }else if(j > 0){ let newValue = arr[j - 1]; if(str1.charAt(i - 1) != str2.charAt(j - 1)) newValue = Math.min(Math.min(newValue, lastValue), arr[j]) + 1; arr[j - 1] = lastValue; lastValue = newValue; } } if (i > 0) arr[str2.length] = lastValue; } return arr[str2.length]; }; console.log(calculateSimilarity('Mathematics','Mathamatecs'));
Output
This will produce the following output −
[ [ 1, 10, 100 ], [ 1, 10, 200 ], [ 1, 10, 300 ], [ 1, 20, 100 ], [ 1, 20, 200 ], [ 1, 20, 300 ], [ 2, 10, 100 ], [ 2, 10, 200 ], [ 2, 10, 300 ], [ 2, 20, 100 ], [ 2, 20, 200 ], [ 2, 20, 300 ] ]
- Related Articles
- Compare two objects in JavaScript and return a number between 0 and 100 representing the percentage of similarity
- Compare two arrays of single characters and return the difference? JavaScript
- Compare return inward and return outward
- Compare Strings in Arduino
- Function to check two strings and return common words in JavaScript
- How to initialize and compare strings?
- How to Initialize and Compare Strings in Java?
- How to Initialize and Compare Strings in C#?
- Compare two Strings in Java
- Compare date strings in MySQL
- How to compare two strings in the current locale with JavaScript?
- What is the best way to compare two strings in JavaScript?
- Compare two strings lexicographically in Java.
- How to compare strings in Java?
- Compare two strings lexicographically in C#

Advertisements