
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Shift strings Circular left and right in JavaScript
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
- Related Articles
- Explain JavaScript Bitwise NOT, Left shift and Right shift?
- Left Shift and Right Shift Operators in C/C++
- What are Left Shift and Right Shift Operators (>> and
- Bitwise Right/ left shift numbers in Arduino
- What is JavaScript Bitwise Left Shift(
- What is unsigned Right Shift Operator (>>>) in JavaScript?
- What is Bitwise Right Shift Operator (>>) in JavaScript?
- What is JavaScript Bitwise Right Shift(>>) Operator?\n
- Shift left in a BigInteger in Java
- Bitwise right shift operators in C#
- Left right subarray sum product - JavaScript
- Segregate all 0s on right and 1s on left in JavaScript
- Shift right in a BigInteger in Java
- What is Bitwise Left Shift Operator (
- What is right shift (>>) operator in Python?
