w3resource

JavaScript: Rotate a string from left to right

JavaScript Basic: Exercise-5 with Solution

Write a JavaScript program to rotate the string 'w3resource' in right direction by periodically removing one letter from the end of the string and attaching it to the front.

Sample Solution:

HTML Code:

<!DOCTYPE html>
  <html> 
  <head>
  <title>JavaScript basic animation</title>
  <script type="text/javascript">
  </script>
  </head> <body onload="animate_string('target')"
  <pre id="target">w3resource </pre>
  </body> 
  </html>
  

JavaScript Code:

function animate_string(id) 
{
    var element = document.getElementById(id);
    var textNode = element.childNodes[0]; // assuming no other children
    var text = textNode.data;

setInterval(function () 
{
 text = text[text.length - 1] + text.substring(0, text.length - 1);
  textNode.data = text;
}, 100);
}

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:

Flowchart: JavaScript - Rotate a string from left to right

ES6 Version:

function animate_string(id) 
{
    const element = document.getElementById(id);
    const textNode = element.childNodes[0]; // assuming no other children
    let text = textNode.data;

setInterval(() => {
 text = text[text.length - 1] + text.substring(0, text.length - 1);
  textNode.data = text;
}, 100);
}

Live Demo:

See the Pen JavaScript current day and time - basic-ex-2 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript function to find the area of a triangle where lengths of the three of its sides are 5, 6, 7.
Next: Write a JavaScript program to determine whether a given year is a leap year in the Gregorian calendar.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



JavaScript: Tips of the Day

Converts a string to title case

Example:

const toTitleCase = str =>
  str
    .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
    .map(x => x.charAt(0).toUpperCase() + x.slice(1))
    .join(' ');
console.log(toTitleCase('some_database_field_name')); // 'Some Database Field Name'
console.log(toTitleCase('Some label that needs to be title-cased')); // 'Some Label That Needs To Be Title Cased'
console.log(toTitleCase('some-package-name')); // 'Some Package Name'
console.log(toTitleCase('some-mixed_string with spaces_underscores-and-hyphens')); // 'Some Mixed String With Spaces Underscores And Hyphens' 

Output:

"Some Database Field Name"
"Some Label That Needs To Be Title Cased"
"Some Package Name"
"Some Mixed String With Spaces Underscores And Hyphens"