Javascript String palindromeAdv()

Description

Javascript String palindromeAdv()

// //  www.  j a v a2s  .  c om
// SUPER advanced way
//
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");
 }
}



PreviousNext

Related