I'm trying to write a function that takes in two string parameters and check if the second parameter is a rotated version of the first string.
So the following would be the results:
checkRotationStrings('waterbottle', 'lewaterbott'); // true
checkRotationStrings('waterbottle', 'bottlewater'); // true
checkRotationStrings('waterbottle', 'erbottlewat'); // true
checkRotationStrings('waterbottle', 'lewaterbottx'); // false
I wrote the following code, but there are some edge cases that I can't seem to figure out:
function checkRotationStrings(string, rotatedString) {
let result;
let rotationCheck
let stringArr = string.split('');
let rotatedStringArr = rotatedString.split('')
for (let i = 0; i < string.length - 1; i++) {
if (rotatedString[0] === stringArr[i]) {
result = stringArr.slice(i);
rotationCheck = stringArr.slice(0, i).concat(result).join('');
}
}
console.log(rotationCheck)
if (rotationCheck === string){
return true;
} else {
return false;
}
}
Any help would be appreciated.