Write a palindrome program in JavaScript so that only alphanumeric values should be allowed?


Palindrome is a string that even when reversed will be the same as the original string. In simple words, the palindrome string will have odd length and the element in 0th index and the last index will be the same, element in 1st index will be the same as the second last element and so on.

The palindrome contains alphanumeric elements; that means it contains the alphabet(a-z) and digits (0-9) as well.

A palindrome string must look like −

var str = "121Malayalam121";
Let’s understand with a suitable example.

Let’s understand with a suitable example.

Example 1

In the above example, you can see how we can check the palindrome string. Initially, we are creating one string, and through the split method we convert a string into an array, and after that, we simply reverse an array and finally join the array.

And using the if condition we can compare both the string.

<!DOCTYPE html> <html> <body> <script> var str = "Hello"; var splitstr = str.split(""); var revstr = splitstr.reverse(); var joinstr = revstr.join(""); if(str == joinstr){ document.write("String is a palindrome string"); } else { document.write("String is not a palindrome string"); } </script> </body> </html>

Using Regular Expressions

Regex expressions are expressions which contain some logical symbol for the character, digit, or special symbol. Through the regex expression, we can match or validate the input values.

Steps to follow

  • First we need to change all the characters of a string to lower case.
  • Later we need to remove all non-alphanumeric values. This task can be done by using inbuilt regular expression named '\W' or we can build our own regular expression.
  • we need to replace all the non-alphanumeric values with nothing(""). This task can be done using an inbuilt method called replace().
  • Once we took the non-alphanumeric values from a string, we need to check whether it reads same in backward and forward directions.
  • we need to reverse the refined string using string.reverse() method and the resulted string should be compared with the original string.
  • If both are equal "true" will be displayed in output else "false" will be displayed.

Example 1

In the example below, we discuss how to perform regex expression to match or replace the non-acceptable value.

<!DOCTYPE html> <html> <body> <script> var str = '@#elle'; var reg = /[\W_]/g; var newstr = str.toLowerCase().replace(reg, ""); document.write(newstr); var strsplit = newstr.split(""); var revstr = strsplit.reverse(); var joinstr = revstr.join(""); if(joinstr === newstr){ document.write("<br>String is palindrome string"); } else { document.write("<br>String is not palindrome string"); } </script> </body> </html>

Example 2

In this example, we are going to take input from a user and decide whether the inputted string is a palindrome or not.

<!DOCTYPE html> <html> <body> <script> var str = prompt('Enter a string'); var reg = /[\W_]/g; var newstr = str.toLowerCase().replace(reg, ""); document.write(newstr); var strsplit = newstr.split(""); var revstr = strsplit.reverse(); var joinstr = revstr.join(""); if(joinstr === newstr){ document.write("<br>String is palindrome string"); } else { document.write("<br>String is not a palindrome string"); } </script> </body> </html>

In the above program, we have used regex expression to match and avoid except alphanumeric values from the input value, and we are taking input from the user.

Example 3

In the given example, we use different regex operations and replace() method to replace some unwanted things like special characters from the string. And after that, we use methods like split(), reverse(), and join() to find palindrome strings. Finally, we use the if condition to checking the string is palindrome or not.

<!DOCTYPE html> <html> <body> <script> var str = prompt('Enter a string'); var reg = /[^A-Za-z0-9]/g; var newstr = str.replace(reg, ""); document.write(newstr); var strsplit = newstr.split(""); var revstr = strsplit.reverse(); var joinstr = revstr.join(""); if(joinstr === newstr){ document.write("<br>String is a palindrome string"); } else { document.write("<br>String is not a palindrome string"); } </script> </body> </html>

Updated on: 29-Aug-2022

442 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements