JavaScript: Rotate a string from left to right
JavaScript Basic: Exercise-5 with Solution
Rotate String 'w3resource' Periodically
Write a JavaScript program to rotate the string 'w3resource' in the right direction. This is done by periodically removing one letter from the string end and attaching it to the front.
The JavaScript program continuously rotates the string 'w3resource' to the right by periodically moving the last character to the front. It uses the setInterval function to update the string every 100 milliseconds, modifying the text content of a specified HTML element by its ID.
Sample Solution:
JavaScript Code:
Live Demo:
Explanation:
document.getElementById(id) : Returns a reference to the element by its ID; the ID is a string which can be used to identify the element; it can be established using the id attribute in HTML, or from script.
Parameters : id is a case-sensitive string representing the unique ID of the element being sought.
element.childNodes[0] will produce the same result as the HTML content of the first child node.
text.length: The length property represents the length of a string. It returns the number of characters units in the string.
Flowchart:

ES6 Version:
For more Practice: Solve these Related Problems:
- Write a JavaScript program that rotates any given string to the left periodically.
- Write a JavaScript program that rotates a string and stops after a certain number of rotations.
- Write a JavaScript program that alternates the rotation direction of a string every few cycles.
Go to:
PREV : Calculate Area of Triangle (Sides: 5, 6, 7).
NEXT : Check Leap Year (Gregorian Calendar).
Improve this sample solution and post your code through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.