Please refer - https://jsfiddle.net/jy5p509c/
var a = "who all are coming 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.
It problem is your
if(a[i] == " ")
condition is not satisfied for the last wordYou can also try a shorter