Skip to content

Instantly share code, notes, and snippets.

@deschantkn
Last active February 28, 2020 16:07
Show Gist options
  • Save deschantkn/0a19012e86548293d6e0685ae1bf086e to your computer and use it in GitHub Desktop.
Save deschantkn/0a19012e86548293d6e0685ae1bf086e to your computer and use it in GitHub Desktop.
Check if a word is a Palindrome
function isPalindrome(word)
{
word = word.toLowerCase();
const length = word.length;
let check = false;
if (word % 2 === 0) {
for (let i = 0; i < length; i++) {
if (word[i] === word[length - i]) {
check = true;
}
}
} else {
const median = Math.ceil(length / 2) - 1;
const wordOdd = word.substring(0, median - 1) + word.substring(median);
for (let i = 0; i < wordOdd.length; i++) {
if (wordOdd[i] === wordOdd[wordOdd.length - i]) {
check = true;
}
}
}
return check;
}
console.log(isPalindrome('bob'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment