通过在 JavaScript 中更改大小写来创建排列
问题
我们需要编写一个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' ]
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短