查找两个字符串中的不同之处 - JavaScript

javascriptweb developmentfront end technologyobject oriented programming更新于 2025/12/6 21:32:17

我们需要编写一个 JavaScript 函数,该函数接受两个字符串并查找字符串中相应不同之处的数量

如果相应元素不相等,则它们将不相似。假设以下是我们的两个字符串 −

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

示例

以下是代码 −

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));

输出

以下是控制台中的输出 −

4

相关文章


有用资源