ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับสตริงอักขระ str เป็นอาร์กิวเมนต์แรกและตัวเดียว
ฟังก์ชันของเราสามารถแปลงตัวอักษรแต่ละตัวเป็นตัวพิมพ์เล็กหรือตัวพิมพ์ใหญ่เพื่อสร้างสตริงอื่น และเราควรส่งคืนรายการสตริงที่เป็นไปได้ทั้งหมดที่เราสามารถสร้างได้
ตัวอย่างเช่น หากอินพุตของฟังก์ชันคือ
ป้อนข้อมูล
const str = 'k1l2';
ผลผลิต
const output = ["k1l2","k1L2","K1l2","K1L2"];
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const str = 'k1l2';
const changeCase = function (S = '') {
const res = []
const helper = (ind = 0, current = '') => {
if (ind >= S.length) {
res.push(current)
return
}
if (/[a-zA-Z]/.test(S[ind])) {
helper(ind + 1, current + S[ind].toLowerCase())
helper(ind + 1, current + S[ind].toUpperCase())
} else {
helper(ind + 1, current + S[ind])
}
}
helper()
return res
};
console.log(changeCase(str)); ผลลัพธ์
[ 'k1l2', 'k1L2', 'K1l2', 'K1L2' ]