【发布时间】:2020-04-11 06:57:51
【问题描述】:
例如:
- moveAllXToEnd("xxre") --> "rexx"
- moveAllXToEnd("xxhixx") --> "hixxxx"
- moveAllXToEnd("xhixhix") --> "hihixxx"
function moveAllXToEnd(str, count = 1) {
if (str.length <= 1) {
return str;
}
// console.log(str.length, count);
if (str.length === count) {
return str;
}
if (str[count] === 'x') {
// Removing the x and putting the '' empty string
let splicedString = str.substr(0, count) + '' + str.substr(count + 1);
// Adding back the 'x' to the end of the string
splicedString += str[count];
return moveAllXToEnd(splicedString, count + 1);
}
return moveAllXToEnd(str, count + 1);
}
【问题讨论】:
标签: javascript node.js string substring coding-style