Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-在句子中反转单词_Javascript_Arrays_Reverse - Fatal编程技术网

Javascript-在句子中反转单词

Javascript-在句子中反转单词,javascript,arrays,reverse,Javascript,Arrays,Reverse,请参阅- var a=“谁都来参加聚会,在什么地方闲逛”; res=“”;resarr=[]; 对于(i=0;i如果(a[i]==“”)最后一个词的条件不满足,则问题在于您的 var a=“谁都来参加聚会,在什么地方闲逛”; res=“”; resarr=[]; 对于(i=0;i

请参阅-

var a=“谁都来参加聚会,在什么地方闲逛”;
res=“”;resarr=[];

对于(i=0;i如果(a[i]==“”)
最后一个词的条件不满足,则问题在于您的

var a=“谁都来参加聚会,在什么地方闲逛”;
res=“”;
resarr=[];
对于(i=0;idocument.body.appendChild(document.createTextNode(res))
我不知道哪一个是最好的答案,我将把它留给你我的,让你决定,它是:

console.log( 'who all are coming to the party and merry around in somewhere'.split('').reverse().join('').split(" ").reverse().join(" "));

在控制台日志之前添加以下行,您将获得预期的结果

res+= resarr.reverse().join("")+" ";
试试这个:

var a = "who all are coming to the party and merry around in somewhere";

//split the string in to an array of words
var sp = a.split(" ");

for (i = 0; i < sp.length; i++) {
    //split the individual word into an array of char, reverse then join 
    sp[i] = sp[i].split("").reverse().join("");
}

//finally, join the reversed words back together, separated by " "
var res = sp.join(" ");

document.body.appendChild(document.createTextNode(res))
var a=“谁都来参加聚会,在什么地方闲逛”;
//将字符串拆分为一个单词数组
var sp=a.split(“”);
对于(i=0;i
它不会反转,因为最后一个字后面没有空格字符。这是因为最后一个字只在[i]中被推送,但不会反转,因为在这个实现中,除了loopbug之外,它太过复杂了。拆分、反转和联接完成了这项工作。
var a = "who all are coming to the party and merry around in somewhere";

//split the string in to an array of words
var sp = a.split(" ");

for (i = 0; i < sp.length; i++) {
    //split the individual word into an array of char, reverse then join 
    sp[i] = sp[i].split("").reverse().join("");
}

//finally, join the reversed words back together, separated by " "
var res = sp.join(" ");

document.body.appendChild(document.createTextNode(res))