Interview Question : Palindrome Simple Demo and Example

Palindrome is very common question in string manipulation,normally interviewer asked to create method to check sting is palindrome or not,in this post i will let you know how to check string is palindrome in JavaScript as well as jquery.
palindrome

Whats is Palindrome ?:

palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.

Checkout other PHP Interview Queations,

Option 1:

Below method is used to check string is palindrome or not using javascript.

Select Code
1
2
3
4
5
6
7
8
9
10
11
function isPalindrome(str) {
                                var len = str.length,
                                                str = str.toLowerCase,
                                                isPalindrome = true;
                                for ( var i = 0; i < Math.floor(len/2); i++ ) {
                                                if (str[i] !== str[len - 1 - i]) {
                                                                isPalindrome = false;;
                                                }
                                }
                                return isPalindrome;
   }

How To use :

simple call above method and passed your string.
alert(isPalindrome('aba'));

Option 2:

Below method is used to check string is palindrome or not using jQuery.

Select Code
1
2
3
4
5
6
7
8
9
function testPalindrome(string) {
                                string = string.toString().toLowerCase();
                                                if (string === string.split('').reverse().join('')) {
                                                                alert(string + ' is palindrome.');
                                                }
                                                else {
                                                                alert(string + ' is not palindrome.');
                                                }
                                }

How To use :

simple call above method and passed your string.
alert(testPalindrome('aba'));