查找字符串中的错误-JavaScript

我们需要编写一个包含两个字符串的JavaScript函数。第一个字符串是某些类型错误的字符串,第二个字符串是此字符串的正确版本。我们可以假设,作为参数获取的两个字符串将始终具有相同的长度。

我们必须返回第一个数组中存在的错误数。

示例

以下是代码-

const str1 = 'Tkos am er exakgrg fetwnh';
const str2 = 'This is an example string';
const countMistakes = (mistaken, correct) => {
   let count = 0;
   for(let i = 0; i < mistaken.length; i++){
      if(mistaken[i] === correct[i]){
         continue;
      };
      count++;
   };
   return count;
};
console.log(countMistakes(str1, str2));

输出结果

以下是控制台中的输出-

15