JavaScript 中两个字符串的差异

javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/30 20:28: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

相关文章


有用资源