如何比我的解决方案更正确地反转初始字符串并节省空间顺序。我需要从初始字符串进行转换,以将其反转但保留与初始字符串相同的空格顺序 'some text with spaces' //=> "seca psht iwtx etemos"
function test(str, result = "") {
let res = str.split('').reverse().join('')
for (let i = 0; i < res.length; i++) {
if (str[i] === " ") {
result += ` ${res[i]}`
str[i + 1];
} else if (str[i] !== " " && res[i] === " ") {
result += ""
} else {
result += res[i];
}
}
return result
}
console.log(test('some text with spaces')) //=> "seca psht iwtx etemos"
慕田峪7331174
不负相思意
温温酱
qq_笑_17
慕村9548890
相关分类