How to check whether a passed string is palindrome or not in JavaScript ?
Last Updated :
20 Dec, 2023
In this article, we are given a string, our task is to find string is palindrome or not. A palindrome is a series of numbers, strings, or letters that, when read from right to left and left to right, match each other exactly or produce the same series of characters. in simple words when number strings or characters are inverted and still provide the same outcome as the original numbers or characters.
Example:
Input: "race"
Output: passed string is not a palindrome
Explanation: if we write "race" in reverse that is "ecar" it not
matches with first string so it is not a palindrome.
Input: "hellolleh"
Output: passed string is palindrome.
These are the following methods by which we can check whether a passed string is palindrome or not:
Approach 1: By using a Naive Approach
- First, we iterate over a string in forward and backward directions.
- Check if all forward and backward character matches, and return true.
- If all forward and backward character does not matches, return false.
- If a return is true, it is a palindrome.
Example: This example shows the use of the above-explained approach.
Javascript
function check_palindrome(str) {
let j = str.length - 1;
for (let i = 0; i < j / 2; i++) {
let x = str[i];
let y = str[j - i];
if (x != y) {
return false ;
}
}
return true ;
}
function is_palindrome(str) {
let ans = check_palindrome(str);
if (ans == true ) {
console.log( "passed string is palindrome " );
}
else {
console.log( "passed string not a palindrome" );
}
}
let test = "racecar" ;
is_palindrome(test);
|
Output
passed string is palindrome
Approach 2: By reversing the string
Another approach is to reverse a string and check if the initial string matches the reverse string or not.
Follow the following steps :
- Initialize reverse_str a variable that stores the reverse of the passed string.
- Compare the string to reverse_str.
- If matches, it is a palindrome.
- The else string is not a palindrome.
Example: This example shows the use of the above-explained approach.
Javascript
function reverse(str) {
let rev_str = "" ;
for (let i = str.length - 1; i >= 0; i--) {
rev_str += str[i];
}
return rev_str;
}
function is_palindrome(str) {
reverse_str = reverse(str);
if (reverse_str === str) {
console.log( "passed string is palindrome " );
}
else {
console.log( "passed string is not palindrome" )
}
}
let test = "hellolleh" ;
is_palindrome(test);
|
Output
passed string is palindrome
Another approach, which is through the shortest approach, uses the split(), reverse(), and join() methods.
- Split the string of characters into several different characters (which is though unsorted at the moment).
- Use the reverse() method to reverse all the characters of the string alphabetically.
- Then apply the join() method in order to join all the characters of the string (which are now sorted).
Example: Below is the implementation of the above approach:
Javascript
let checkPalindrome = (stringg) => {
return stringg === stringg.split( "" ).reverse().join( "" );
};
console.log( "Is Palindrome? : " + checkPalindrome( "noon" ));
console.log( "Is Palindrome?: " + checkPalindrome( "apple" ));
|
Output
Is Palindrome? : true
Is Palindrome?: false
Share your thoughts in the comments
Please Login to comment...