เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ยอมรับสองสตริงและตัวเลข n ฟังก์ชันจะจับคู่สองสตริง กล่าวคือ จะตรวจสอบว่าสตริงทั้งสองมีอักขระเหมือนกันหรือไม่ ฟังก์ชันควรคืนค่าเป็น true หากสตริงทั้งสองมีอักขระเดียวกันโดยไม่คำนึงถึงลำดับ หรือมีอักขระต่างกันไม่เกิน n ตัว มิฉะนั้น ฟังก์ชันควร returnfalse
มาเขียนโค้ดสำหรับฟังก์ชันนี้กัน −
ตัวอย่าง
const str1 = 'first string';
const str2 = 'second string';
const wildcardMatching = (first, second, num) => {
let count = 0;
for(let i = 0; i < first.length; i++){
if(!second.includes(first[i])){
count++;
};
if(count > num){
return false;
};
};
return true;
};
console.log(wildcardMatching(str1, str2, 2));
console.log(wildcardMatching(str1, str2, 1));
console.log(wildcardMatching(str1, str2, 0)); ผลลัพธ์
ผลลัพธ์ในคอนโซลจะเป็น -
true true false