How to use Iterative Approach In JavaScript

Example:

JavaScript
function isSymmetric(str) {
    let left = 0;
    let right = str.length - 1;

    while (left < right) {
        if (str[left] !== str[right]) {
            return false;
        }
        left++;
        right--;
    }

    return true;
}

let inputStr = "abcdcba";

if (isSymmetric(inputStr)) {
    console.log("The given string is Symmetric");
} else {
    console.log("The given string is Not Symmetric");
}

Output
The given string is Symmetric

JavaScript Program to Check Whether the String is Symmetrical or Not

In this article, we will see how to check whether the given string is symmetric or not. The symmetrical string is one that is similar from the starting to mid and mid to end.

Example:

Input: khokho
Output: 
The entered string is symmetrical
Input: madam
Output: 
The entered string is not symmetrical
Input:amaama
Output:
The entered string is symmetrical

Similar Reads

Methods to Check Whether the Given String is Symmetric or Not

Table of Content Method 1: Naive method using loopsMethod 2: Using the slice methodMethod 3: Using substring methodMethod 4: Using Iterative ApproachMethod 5: Using split, reverse, and join methods...

Method 1: Naive method using loops

Example:...

Method 2: Using the slice method

Example:...

Method 3: Using substring method

Javascript let str = "abcdabc"; mid = Math.floor(str.length / 2); if ( str.substring(0, mid) === str.substring(mid + (str.length % 2 === 0 ? 0 : 1)) ) console.log("The given string is Symmetric"); else { console.log("The given string is Not Symmetric"); }...

Method 4: Using Iterative Approach

Example:...

Method 5: Using split, reverse, and join methods

This method involves splitting the string into two halves, reversing the second half, and then comparing the two halves....

Contact Us