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