Differences in two strings in JavaScript


We are required to write a JavaScript function that takes in two strings and find the number of corresponding dissimilarities in the strings. The corresponding elements will be dissimilar if they are not equal

Example

Let’s say the following are our strings −

const str1 = 'Hello world!!!';
const str2 = 'Hellp world111';

Example

The code for this will be −

const str1 = 'Hello world!!!';
const str2 = 'Hellp world111';
const dissimilarity = (str1 = '', str2 = '') => {
   let count = 0;
   for(let i = 0; i < str1.length; i++){
      if(str1[i] === str2[i]){
         continue;
      };
      count++;
   };
   return count;
};
console.log(dissimilarity(str1, str2));

Output

The output in the console will be −

4

Updated on: 17-Oct-2020

340 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements