Skip to content

Instantly share code, notes, and snippets.

@alexeykomov
Created October 5, 2016 14:03
Show Gist options
  • Save alexeykomov/49983ef8bd66a2e21c05952ccbf23abd to your computer and use it in GitHub Desktop.
Save alexeykomov/49983ef8bd66a2e21c05952ccbf23abd to your computer and use it in GitHub Desktop.
const palyndrome = 'laal';
function reverseString(str) {
return Array.prototype.reduceRight.call(str, (acc, newElement) =>
acc + newElement, '');
}
function isPalyndrome(str) {
const length = str.length;
if (length === 0 || length === 1) {
return true;
} else if (length % 2 === 0) {
const middle = length / 2;
return str.substr(0, middle) === reverseString(str.substr(middle, length));
} else {
const middle = Math.floor(length / 2);
return str.substr(0, middle) === reverseString(str.substr(middle + 1, length));
}
}
console.log(isPalyndrome(palyndrome));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment