문제
문자열 str을 첫 번째이자 유일한 인수로 취하는 JavaScript 함수를 작성해야 합니다.
우리 함수는 모든 문자를 개별적으로 소문자 또는 대문자로 변환하여 다른 문자열을 생성할 수 있습니다. 그리고 우리가 만들 수 있는 모든 가능한 문자열의 목록을 반환해야 합니다.
예를 들어 함수에 대한 입력이
인 경우입력
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' ]