通过在 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' ]
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下