Nodejs String Palindrome Check palindrome()

Here you can find the source of palindrome()

Method Source Code

String.prototype.palindrome = function() {
  var palindrome = ["forwards", "backwards"];
  for (var i = 0; i < palindrome.length; i++) {
    var index = this.indexOf(palindrome[i]);
    if(index >= 0) {
      return true;
    }// w w  w  .  ja  v  a  2  s  .co m
  }
  return false;
};

var sentences = ["I'll send my car around to pick you up.",
                  "Let's touch base in the morning forwards",
                  "We dont backwards."];

for (var i = 0; i < sentences.length; i++) {
  var phrase = sentences[i];
  if (phrase.palindrome()){
    console.log("PALINDROME ALERT " + phrase);
  }
}

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('');