如何在一个句子中颠倒词序 [英] How to reverse word order in a sentence
问题描述
我写了一个代码来反转一个句子中的单词顺序。测试句是引号,我需要在我的解决方案中加上引号,但似乎无法正确添加....
ex。
测试:去咖啡店
我的代码返回:coffeeshop去Go
但它需要回复:coffeeshop to Go
我尝试过:
嗨!我写了一个用于在一个句子中反转单词顺序的代码(不使用反向方法)并且得到了这个:
I wrote a code to reverse the word order in a sentence. The test sentence was in quotation marks and I need to have quotation marks in my solution but can't seem to add it in properly....
ex.
Test: "Go to the coffeeshop"
my code returns this: coffeeshop the to Go
But it needs to return this: "coffeeshop the to Go"
What I have tried:
Hi! I wrote a code for reversing word order in a sentence (without using the reverse method) and got this:
function reverseSent(sentence){
var result = " ";
var splitStr = sentence.split(" ");
for (var i = splitStr.length - 1; i >= 0; i--){
result += splitStr[i] + " ";
var words = result;
}
return words;
}
- 订单是正确的,但我在句子的开头和结尾都没有引号。如何添加它们?
-the order is right, but I'm missing the quotation marks at the beginning and end of the sentence. How can I add them in?
推荐答案
function reverseSent(sentence){
return sentence.split(/\b/).reverse().join('')
}
这篇关于如何在一个句子中颠倒词序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!