We are required to write a JavaScript function that takes in three arguments, first is a string, say str, then we have two numbers, lets say m and n. The numbers m and n basically specify the amount of leftShifts and rightShifts respectively.
We define these terms like such −
Left Shift − A single circular rotation of the string in which the first character becomes the last character and all other characters are shifted one index to the left.
For example, abcde becomes bcdea after one left shift and cdeab after two left shifts.
Right Shift − A single circular rotation of the string in which the last character becomes the first character and all other characters are shifted to the right.
For example, abcde becomes eabcd after one right shift and deabc after two right shifts.
So, basically our function should perform the specified number of left and right shifts and then finally return the resultant string.
Example
The code for this will be −
const str = 'abcdef'; const getShiftedString = (str, leftShifts, rightShifts) => shiftByAmount(shiftByAmount(str, leftShifts), −rightShifts); // helper function // negative amount shifts to right // positive amount shifts to left const shiftByAmount = (str, leftShifts) => { leftShifts = leftShifts % str.length; return str.slice(leftShifts) + str.slice(0, leftShifts); }; console.log(getShiftedString(str, 3, 2));
Output
And the output in the console will be −
Bcdefa