最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Javascript - Reverse words in a sentence - Stack Overflow

programmeradmin1浏览0评论

Please refer - /

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

res = ""; resarr = [];

for(i=0 ;i<a.length; i++) {

if(a[i] == " ") {
    res+= resarr.reverse().join("")+" ";
    resarr = [];
}
else {
    resarr.push(a[i]);
}   
}
console.log(res);

The last word does not reverse and is not outputted in the final result. Not sure what is missing.

Please refer - https://jsfiddle/jy5p509c/

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

res = ""; resarr = [];

for(i=0 ;i<a.length; i++) {

if(a[i] == " ") {
    res+= resarr.reverse().join("")+" ";
    resarr = [];
}
else {
    resarr.push(a[i]);
}   
}
console.log(res);

The last word does not reverse and is not outputted in the final result. Not sure what is missing.

Share Improve this question asked Jun 16, 2015 at 11:00 gopal raogopal rao 2931 silver badge12 bronze badges 4
  • 1 stackoverflow./questions/958908/… – user3272018 Commented Jun 16, 2015 at 11:01
  • 2 It doesn't reverse because there's no space character after the last word. – Lye Fish Commented Jun 16, 2015 at 11:02
  • Its because last word is only getting pushed in a[i] but not getting reversed since ing out of for loop – shreyansh Commented Jun 16, 2015 at 11:07
  • Bugs in this implementation aside, it's far too overplicated. Split, reverse, and join does the job. – AJF Commented Jun 16, 2015 at 12:11
Add a ment  | 

4 Answers 4

Reset to default 10

It problem is your if(a[i] == " ") condition is not satisfied for the last word

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

res = "";
resarr = [];

for (i = 0; i < a.length; i++) {
  if (a[i] == " " || i == a.length - 1) {
    res += resarr.reverse().join("") + " ";
    resarr = [];
  } else {
    resarr.push(a[i]);
  }
}

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


You can also try a shorter

var a = "who all are ing to the party and merry around in florida";

var res = a.split(' ').map(function(text) {
  return text.split('').reverse().join('')
}).join(' ');

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

I don't know wich one is the best answer I'll live you mine and let you decide, here it is :

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

Add the following line before console log, you will get as expected

res+= resarr.reverse().join("")+" ";

Try this:

var a = "who all are ing 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))
发布评论

评论列表(0)

  1. 暂无评论