Created
May 19, 2020 18:20
-
-
Save ivancp/e9065101ef1da83a27cb86d676a67ae2 to your computer and use it in GitHub Desktop.
String.prototype.rotate from @js_tut challenge
This file contains hidden or 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
/* https://twitter.com/js_tut/status/1262754711502942213 */ | |
String.prototype.rotate = function(shiftVal){ | |
var str = this.toLowerCase(); | |
var newPos = shiftVal > 0 ? (str.length - (shiftVal % str.length)):(Math.abs(shiftVal) % str.length); | |
if(newPos == str.length) | |
return str.charAt(0).toUpperCase() + (str.length > 1 ? str.substring(1,str.length):''); | |
return str.charAt(newPos).toUpperCase() + str.substring(newPos+1,str.length) + str.substring(0,newPos); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment