Skip to content

Instantly share code, notes, and snippets.

Created November 19, 2016 19:13
Show Gist options
  • Save anonymous/8f9e9a059cf5353d4bc52ee63a4d8a53 to your computer and use it in GitHub Desktop.
Save anonymous/8f9e9a059cf5353d4bc52ee63a4d8a53 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/qezako
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
'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.'));
</script>
<script id="jsbin-source-javascript" type="text/javascript">function reverseWords(str){
let reversedString = [],
wordLen = 0;
for(let i = str.length - 1; i >= 0; i--){
if(str[i] == ' ' || i === 0){
let 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.'));
</script></body>
</html>
'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