Reverse words in a string: Difference between revisions

No edit summary
Line 937:
 
=={{header|JavaScript}}==
<lang javascript>var strReversed =
"---------- Ice and Fire ------------\n\
\n\
fire, in end will world the say Some\n\
ice. in say Some\n\
desire of tasted I've what From\n\
fire. favor who those with hold I\n\
\n\
... elided paragraph last ...\n\
\n\
Frost Robert -----------------------";
function reverseString(s) {
return s.split('\n').map(function(line) {
returnfunction (line.split(/\s/).reverse().join(' ');{
} return line.split(/\s/).reverse().join('\n ');
}
).join('\n');
}
</lang>
console.log(
reverseString(strReversed)
);</lang>
 
Output:
<pre>------------ Fire and Ice ----------
 
Some say the world will end in fire,
Some say in ice.
From what I've tasted of desire
I hold with those who favor fire.
 
... last paragraph elided ...
 
----------------------- Robert Frost</pre>
 
=={{header|jq}}==
9,659

edits