// Sample solution
// Write a string rotation method.
// It should pass all the tests given below.
String.prototype.rotate = function(number) {
/* your code */
if(number===0 || number===this.length){
return this;
} else if(this.length>Math.abs(number)){
if(number<0){
number = Math.abs(number)+1;
}
} else if(this.length<Math.abs(number)){
if(number>0){
number = (Math.abs(number) % this.length)+2;
} else {
number = (Math.abs(number) % this.length)+1;
}
}
var temp1 = this.substring(0,number-1);
var temp2 = this.substring(number-1,this.length);
return temp2+temp1; // return rotated string
};
/*********** Tests for your method ***********/
// The comments tell you the expected results.
// The correctness test will run all these tests, so even if you
// delete the lines below, you will still see error messages
// as long as your code doesn't pass all the tests.
console.log("ryEve".rotate(3)); // "Every"
// Zero rotation: no change
console.log("JavaScript".rotate(0)); // "JavaScript"
// Negative numbers: rotate the other way
console.log("mmerprogra".rotate(-4)); // "programmer"
// Rotate by length: no change
console.log("should".rotate(6)); // "should"
console.log("know".rotate( -4)); // "know"
// Larger than length: rotate by modulo
console.log("outab".rotate(22)); // "about"
// (actually the same as "outab".rotate(2); )
console.log("otypesprot".rotate(-36)); // "prototypes"
// (actually the same as "otypesprot".rotate(-6); )