Palindrome in JavaScript – GeeksforGeeks

Reinforce Article

Save Article

Like Article

Reinforce Article

Save Article

Like Article

We can know how to test whether or not a given worth is a palindrome or now not in JavaScript. A palindrome is a worth that reads the similar from backward or ahead. To accomplish this test we will be able to use the next approaches:

Means 1: 

  • Iterate over the string from ahead and backward course
  • Take a look at if the nature fit
  • Go back false for the primary personality that doesn’t fit 
  • Go back true if the entire comparisons are carried out and the characters fit

Instance: This situation implements the above method.

Javascript

serve as isPalindrome(str) {

    var j = str.length-1

    for(var i=0; i<str.size/2; i++){

        if(str[i]!=str[j]){

            go back false;

        }

        j--;

    }

    go back true;

}

  

var str1 = "racecar";

var str2 = "nitin";

var str3 = "Rama";

  

console.log(isPalindrome(str1));

console.log(isPalindrome(str2));

console.log(isPalindrome(str3));

Output:

true
true
false

Means 2:

  • Initialize a variable and retailer the opposite of the price in it the use of for loop
  • Evaluate the unique worth with the reversed worth
  • If each values are equivalent go back true else go back false

Instance: This situation implements the above method.

Javascript

serve as isPalindrome(str) {

    var rev="";

    for(var i=str.length-1; i>=0; i--){

        rev+= str[i];

    }

    if(rev==str){

        go back true

    } else{

        go back false;

    }

}

  

var str1 = "racecar";

var str2 = "nitin";

var str3 = "Rama";

  

console.log(isPalindrome(str1));

console.log(isPalindrome(str2));

console.log(isPalindrome(str3));

Output:

true
true
false

Means 3: 

  • Use in-built string strategies like cut up() to separate the string into characters array
  • Opposite the array the use of opposite() approach
  • Sign up for the array right into a string the use of sign up for() approach
  • Retailer this worth within some other variable
  • Evaluate the values and go back true if each are equivalent

Instance: This situation implements the above method on string

Javascript

serve as isPalindrome(str) {

    var rev=str.cut up("").opposite().sign up for("");

  

    if(rev == str){

        go back true

    }

    go back false

                 

}

  

var str1 = "racecar";

var str2 = "nitin";

var str3 = "Rama";

  

console.log(isPalindrome(str1));

console.log(isPalindrome(str2));

console.log(isPalindrome(str3));

Output:

true
true
false

Instance: This situation implements the above way to Quantity

Javascript

serve as isPalindrome(num) {

    var str = num.toString();

    var rev=str.cut up("").opposite().sign up for("");

  

    if(rev == str){

        go back true

    }

    go back false

                 

}

  

var str1 = 1234321;

var str2 = 112211;

var str3 = 12345;

  

console.log(isPalindrome(str1));

console.log(isPalindrome(str2));

console.log(isPalindrome(str3));

Output: The Quantity is first transformed to a string after which when put next the use of the former method

true
true
false

To grasp extra about test whether or not a handed string is palindrome or now not in JavaScript?

Like Article

Save Article

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: