
- 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
Check if a string is repeating in itself in JavaScript
We are required to write a JavaScript function that takes in a string as the first and the only argument.
The function should detect if the string is a repetition of a same set of characters or not.
If it is a repetition of the same set of characters then we should return true, false otherwise.
For example −
If the input string is −
const str = 'carcarcarcar';
Then the output should be −
const output = true;
because the string 'car' is getting repeated over and over again in the string.
Example
Following is the code −
const str = 'carcarcarcar'; const isRepeating = (str = '') => { if (!str.length){ return false }; for(let j = 1; (j <= str.length / 2); j++){ if (str.length % j != 0){ continue }; let flag = true; for(let i = j; i < str.length; ++ i){ if(str[i] != str[i - j]){ flag = false; break; }; }; if(flag){ return true; }; }; return false; }; console.log(isRepeating(str));
Output
Following is the console output −
true
- Related Articles
- Check if a string is sorted in JavaScript
- Program to check the string is repeating string or not in Python
- Repeating letter string - JavaScript
- Check if a string has white space in JavaScript?
- Check if user inputted string is in the array in JavaScript
- Return index of first repeating character in a string - JavaScript
- How can I tell if a string repeats itself in Python?
- JavaScript - Find if string is a palindrome (Check for punctuation)
- Check if string begins with punctuation in JavaScript
- Check if a given string is sum-string in C++
- Check if a string is Colindrome in Python
- Finding the first non-repeating character of a string in JavaScript
- Detecting the first non-repeating string in Array in JavaScript
- Inserting empty string in place of repeating values in JavaScript
- Check if the string is a combination of strings in an array using JavaScript

Advertisements