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