Nodejs String Palindrome Check palindrome(str)

Here you can find the source of palindrome(str)

Method Source Code

/*Check for Palindromes 
Return true if the given string is a palindrome. Otherwise, return false.

A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.

Note//from w w w  .j av  a  2s .  c o  m
You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything lower case in order to check for palindromes.

We'll pass strings with varying formats, such as "racecar", "RaceCar", and "race CAR" among others.

We'll also pass strings with special symbols, such as "2A3*3a2", "2A3 3a2", and "2_A3*3#A2".

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:

String.prototype.replace()
String.prototype.toLowerCase()*/
function palindrome(str) {
   /* remove special characters, spaces and make lowercase*/
  var removeChar = str.replace(/[^A-Z0-9]/ig, "").toLowerCase();
//alert(str.split('').reverse().join(''));
 /* reverse removeChar for comparison*/
  var checkPalindrome = removeChar.split('').reverse().join('');

  /* Check to see if str is a Palindrome*/
   return (removeChar === checkPalindrome);

  
}



palindrome("eye");

Related

  1. palindrome()
    String.prototype.palindrome = function() {
      var len = this.length-1;
      for (var i = 0; i <= len; i++) {
        if (this.charAt(i) !== this.charAt(len-i)) {
          return false;
        if (i === (len-i)) {
          return true;
      return true;
    };
    String.prototype.palindromeAdv = function() {
      var r = this.split("").reverse().join("");
      return (r === this.valueOf());
    var phrases = ["eve",
                   "kayak",
                   "mom",
                   "wow",
                   "noon",
                   "Not a palindrome"];
    for (var i = 0; i < phrases.length; i++) {
      var phrase = phrases[i];
      if (phrase.palindrome()) {
        console.log("'" + phrase + "' is a palindrome");
      } else {
        console.log("'" + phrase + "' is NOT a palindrome");
    
  2. palindrome()
    String.prototype.palindrome = function() {
        for (var i = 0; i <= this.length - 1; i++) {
            if (this.charAt(i) !== this.charAt(len - i)) {
                return false;
            if (i === (len - i)) {
                return true;
        return true;
    };
    String.prototype.palindromeAdv = function() {
        var r = this.split("").reverse().join("");
        return (r === this.valueOf());
    };
    
  3. palindrome()
    'use strict';
    String.prototype.palindrome = String.prototype.palindrome || function ( ) {
      if ( typeof this == 'string' ) {
        return this === this.split('').reverse().join('');
      else {
        return false;
    };
    ...
    
  4. palindrome(loose)
    String.prototype.palindrome = function(loose) {
        var str = loose ? this.replace(/[^a-zA-Z0-9]+/gi, '').toLowerCase() : this;
        return str == str.split('').reverse().join('');
    
  5. palindrome(str)
    function palindrome(str) {
      str = str.replace(/[^a-zA-Z0-9]+/gi, '').toLowerCase();
        return str == str.split('').reverse().join('');
    palindrome("eye");
    
  6. palindrome(str)
    String.prototype.reverse = function(){ return this.split("").reverse().join(""); }
    function palindrome(str) { return str == str.reverse(); }
    console.log(palindrome("ingirumimusnocteetconsumimurigni"));
    
  7. isPalindrome()
    String.prototype.isPalindrome = function() {
      var str = this;
      var half = parseInt(str.length / 2, 10);
      for (var i = 0, last = str.length - 1; i < half; ++i) {
        if (str[i] != str[last - i]) return false;
      return true;
    };
    
  8. isPalindrome()
    String.prototype.isPalindrome=function(){
      var palavra=this;
      for(var i=0;i<palavra.length/2;i++){
        if(palavra[i]!=palavra[palavra.length-1-i]){
          return false;
      return true;
    
  9. isPalindrome()
    String.prototype.isPalindrome = function() {
      var val = this.toString().toLowerCase();
      var reverse = val.split('').reverse().join('');
      return val === reverse;
    var str = 'something';
    var result = str.isPalindrome();  
    var str2 = 'level';
    var result2 = str2.isPalindrome();  
    ...