Skip to content

Instantly share code, notes, and snippets.

@albinotonnina
Forked from anonymous/index.html
Last active November 19, 2016 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albinotonnina/993166e1292dd361bc4b738abf09951d to your computer and use it in GitHub Desktop.
Save albinotonnina/993166e1292dd361bc4b738abf09951d to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/qezako
'use strict';
function reverseWords(str) {
var reversedString = [],
wordLen = 0;
for (var i = str.length - 1; i >= 0; i--) {
if (str[i] == ' ' || i === 0) {
var word = str.substr(i, wordLen + 1).trim();
reversedString.push(word);
wordLen = 0;
} else {
wordLen++;
}
}
return reversedString.join(' ');
}
console.log(reverseWords('The real voyage of discovery consists not in seeking new lands but seeing with new eyes.'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment