ปัญหา
เราต้องเขียนฟังก์ชัน 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' ]