【发布时间】:2020-05-07 15:59:00
【问题描述】:
我已经在这个函数上工作了一段时间,但我无法弄清楚为什么即使我使用 .splice() 我也没有得到修改后的数组。 我提供了开始更改数组“i”的索引、要删除的元素数量“1”和要添加的元素“str[i]”。
function wave(str) {
let result = [];
for (let i = 0; i < str.length; i++) {
if ((/[a-z]/ig).test(str[i])) {
let st = str.split("").splice(i, 1 , str[i].toUpperCase());
result.push(st);
}
}
return result;
}
console.log(wave('hello')); // expected ["Hello", "hEllo", "heLlo", "helLo", "hellO"];
console.log(wave("two words")); // ["Two words", "tWo words", "twO words", "two Words", "two wOrds", "two woRds", "two worDs", "two wordS"];
【问题讨论】:
标签: javascript for-loop splice array-splice