Nodejs String Palindrome Check palindrome()

Here you can find the source of palindrome()

Method Source Code

'use strict';//from w w w. ja v a2 s . c  o m

// Define a repeatify function on the String object. 
// The function accepts an integer that specifies how many times the string has to be repeated.
// The function returns the string repeated the number of times specified. 
// For example: console.log('hello'.repeatify(3));
// Should print hellohellohello.


String.prototype.palindrome = String.prototype.palindrome || function ( ) {

   if ( typeof this == 'string' ) {

      return this === this.split('').reverse().join('');
      
   }
   else {

      return false;
   }

};
String.prototype.repeatify = String.prototype.repeatify || function ( times ) {

   var str = '';

   for ( var i = 0; i < times; i++ ) {
      
      str += this; 
   };

   return str;
};


// Make console.log('i am all lowercase'.capitalize()); to print 'I am all lowercase'

String.prototype.capitalize = String.prototype.capitalize || function ( ) {

   return this.charAt(0).toUpperCase() + this.slice(1);

};


console.log('i am all lowercase'.capitalize());

console.log('hello'.repeatify(3));

console.log('lol'.palindrome());
console.log('loll'.palindrome());

Related

  1. palindrome()
    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;
      return false;
    ...
    
  2. 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");
    
  3. 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());
    };
    
  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)
    function palindrome(str) {
      var removeChar = str.replace(/[^A-Z0-9]/ig, "").toLowerCase();
      var checkPalindrome = removeChar.split('').reverse().join('');
       return (removeChar === checkPalindrome);
    palindrome("eye");
    
  7. palindrome(str)
    String.prototype.reverse = function(){ return this.split("").reverse().join(""); }
    function palindrome(str) { return str == str.reverse(); }
    console.log(palindrome("ingirumimusnocteetconsumimurigni"));