เราจำเป็นต้องเขียนฟังก์ชัน 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