JSFiddle - React, Tailwind, and code Playground
by Bhavik Bamania
JavaScript
// Rotate a word by integer given
const str = "Bhavik", k = 2;
function stringRotator(str, rotation) {
console.log("there")
let newStr="";
for(let i = rotation; i < str.length; i++) {
newStr += str[i];
}
for(let i = 0; i < rotation; i++) {
newStr+= str[i];
}
return newStr;
}
function stringRotator(str, rotation) {
console.log("here")
if(!str) return str;
const n = rotation % str.length;
const newStr = str.slice(n) + str.slice(0, k);
return newStr
}
console.log(stringRotator(str, k));