JavaScript 中两个字符串的差异
我们需要编写一个 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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP