This function will rotate a string either forwards or backwards, and does so in 9155 bytes.
For example, "hello" shifted by 3 is "llohe" and "123456789" shifted by -37 is "234567891".
-
-
Save xpansive/1910542 to your computer and use it in GitHub Desktop.
rotateString in 55 bytes
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
function(a, // The text to rotate | |
b) // The amount to shift by | |
{ | |
return | |
a.slice(b%=a.length) // Get a slice starting at the shift amount and going to the end of the string | |
// Also modulo shift by the text length to make sure we stay inside | |
+a.slice(0,b) // Add on a slice from the beginning of the string to the shift amount | |
} |
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
function(a,b){return a.slice(b%=a.length)+a.slice(0,b)} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Noah Weninger <https://github.com/xpansive> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "rotateString", | |
"description": "Rotates the text in strings either forwards or backwards.", | |
"keywords": [ | |
"string", | |
"rotate", | |
"shift" | |
] | |
} |
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
<!DOCTYPE html> | |
<title>rotateString example</title> | |
<pre id=out onmouseover="inc=-1" onmouseout="inc=1"></pre> | |
<script> | |
var rotateString = function(a,b){return a.slice(b%=a.length)+a.slice(0,b)}; | |
var shift = 0, inc = 1; | |
setInterval(function() { | |
document.getElementById("out").innerHTML = rotateString("140byt.es rules! ", shift += inc); | |
}, 100); | |
</script> |
Save 5 bytes more by becoming even more obvious...
function(a){return a.split('').reverse().join('')}
rotate
is more like shift
. reverse
is a different function. If you want to do this with array functions it would be something like this:
function(a,b){for(a=a.split('');b--;)a.push(a.shift());return a.join('')}
Sorry, missed that.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Saved 36 bytes by rewriting it the obvious way...