Javascript String palindrome()
String.prototype.palindrome = function () { var word = this; console.info(word.match(/[a-zA-Z]/g).join('') +' = '+ word.match(/[a-zA-Z]/g).reverse().join('')); return word.match(/[a-zA-Z]/g).join('') === word.match(/[a-zA-Z]/g).join(''); }; 'Anaaaa'.palindrome();
String.prototype.palindrome = 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 {/*from w ww . jav a 2s . com*/ console.log("'" + phrase + "' is NOT a 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; }/* w w w .j a v a 2s .com*/ } 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); } }
String.prototype.palindrome = function(){ // Advanced Solution // we need to use valueOf() because 'this' is a function // var inputArray = this.split('').reverse().join(''); // return (inputArray === this.valueOf()); // };/*from w ww. j a va 2 s . com*/ 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; }; var phrases = ["eve", "kayak", "mom", "wow", "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."); } }
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;/*from w w w .jav a2 s . c om*/ } if (i === (len-i)) { return true; } } return true; };