我可以问我的第一个问题。抱歉,标题太差了。我有点烦。因此,我构建了一个函数来反转具有5个或更多字符的字符串,如下所示。
function spinWords(str) {
str2 = str.split(" ");
str3 = [];
for (i = 0; i < str2.length; i++) {
str3 = str2[i].split("");
if (str3.length >= 5) {
str3.reverse();
}
str4 = str3.join("");
return str4;
}
}
spinWords("Welcome To The Club");`
我期望的输出是这样的
emocleW To The Club
但是代码输出是这个
emocleW
To
The
Club
是否有任何解决方案,至少将四个迭代字符串合并为一行?
每个帮助将是非常好的。谢谢!!!
function spinWords(str) {
function reverseString(str) {
return str.split("").reverse().join("");
}
const words = str.split(" ");
const spinnedWords = words.map(word => {
if (word.length >=5 ) return reverseString(word);
else return word;
});
return spinnedWords.join(' ');
}
spinWords("Welcome To The Club");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句