JSFiddle - React, Tailwind, and code Playground

by ronilan

HTML

// String.prototype.isPalindrome

JavaScript

String.prototype.isPalindrome = function () {

    var i,
    max = Math.floor(this.length / 2),
        result = true;

    for (i = 0; i < max; i++) {
        if (this.substr(i, 1) !== this.substr(this.length - i - 1, 1)) {
            result = false;
        }
    }

    return result;
};

// true
console.log("".isPalindrome());

// true
console.log("a".isPalindrome());

// true
console.log("abcdcba".isPalindrome());

// true
console.log("abcddcba".isPalindrome());

// false
console.log("axbcdcba".isPalindrome());

// false
console.log("hello world".isPalindrome());