-
-
Save albinotonnina/993166e1292dd361bc4b738abf09951d to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/qezako
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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