Javascript String isPalindrome()
//EX1//from w w w. j av a2 s . com 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; } //EX2 Date.prototype.bissexto=function(){ var ano=this.getFullYear(); if(ano % 4 === 0 && (ano % 100 != 0) || (ano % 400 === 0)){ return true; } return false; }
String.prototype.isPalindrome = ()=>{ return this.replace(/[^a-zA-Z ]/g, "").replace(/ /g,'').trim().toLowerCase() == this.replace(/[^a-zA-Z ]/g, "").replace(/ /g,'').trim().toLowerCase().split('').reverse().join(''); }
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; }/* www. j a va 2 s . c o m*/ return true; };
/* return if a string is a palindrome */ String.prototype.isPalindrome = function() { var str = this.replace(/\W/g, ''); return str.toLowerCase() === str.toLowerCase().split('').reverse().join(''); }; var isPal = 'a man a plan a canal panama'.isPalindrome(); console.log(isPal); //true
// Monkeypatching is the act of adding functionality onto an object that // wouldn't ordinarily be there, or to override existing functionality. 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(); // false var str2 = 'level'; var result2 = str2.isPalindrome(); // true
'use strict'//from w w w .j a va 2s . c o m function isPalindrome(str) { var replStr = str.replace(/[.,"'\/#!$%\^&\*;:{}=\-_`~()\s]/g, "").toLowerCase(); var revStr = replStr.split('').reverse().join(''); //console.log(replStr); //console.log(revStr); if (replStr === revStr) { return true; } else { return false; } }; String.prototype.isPalindrome = String.prototype.isPalindrome || isPalindrome;
// 004 Find the largest palindromic product of two three-digit numbers var first = 999,//from w ww. j a va 2 s . c o m second = 999, largestPalindromicProduct = 0; String.prototype.isPalindrome = function() { return this == this.reverse(); } String.prototype.reverse = function() { return this.split("").reverse().join(""); }; while(first > 99) { second = first; while(second > 99) { var product = first * second; if(product.toString(10).isPalindrome() && product > largestPalindromicProduct) largestPalindromicProduct = product; second--; } first--; } console.log(largestPalindromicProduct);
String.prototype.isPalindrome = function () { // Note that a primitive JavaScript string is not === to a String object. // The valueOf function returns the underlying primitive. return this.valueOf() === this.reverse().valueOf(); }; String.prototype.reverse = function () { return Array.prototype.slice.apply(this).reverse().join(''); }; let result = 0;//from ww w .j a va 2s .c o m for (let i = 999; i > 0; i--) { for (let j = i; j > 0; j--) { let x = i * j; if (x.toString().isPalindrome() && x > result) result = x; } } print(result);
String.prototype.isPalindrome = function () { var s = this.toLowerCase().replace(/[^a-z]/g, ''); return (s.reverse() === s); }; String.prototype.reverse = function () { return this.split('').reverse().join(''); };
var isPalindrome = function(str){ var i = 0;/* w w w .j ava 2 s . c o m*/ var j = str.length-1; while (i <= j){ if (str[i] != str[j]){ return false; } i++; j--; } return true; } var isPalindrome2 = function(string){ for (var i = 0, j = string.length-1; i < string.length/2; i++, j--){ if (string[i] != string[j]){ return false; } } return true; } String.prototype.isPalindrome = function(){ // return isPalindrome(this); var i = 0; var j = this.length-1; while (i <= j){ if (this[i] != this[j]){ return false; } i++; j--; } return true; }
//Scooter's Solution var isPalindrome = function(inString) { var inString = inString.toLowerCase(); //(var i = 0; i < string.length/2; i++) for (var i = 0, j = inString.length-1; i < inString.length/2; i++, j--) { //if (string[i] !== string[string.length-(i+1)] ) if (inString[i] != inString[j]){ return false;// w w w .ja va 2s .com } } return true; } //Chucks Solution String.prototype.isPalindrome = function isPalindrome() { console.log(this); var new_str = this.toString().replace(" ", ""); if (new_str == new_str.split("").reverse().join("")) { return true; } else { return false; } }
// Example - extending a method in String String.prototype.isPalindrome = function() { return this.replace(/[^A-Z0-9]/ig, "").toLowerCase() === this.split("").reverse().join(""); }; console.log("madam".isPalindrome()); // true console.log("str".isPalindrome()); // false