Last active
February 28, 2020 16:07
-
-
Save deschantkn/0a19012e86548293d6e0685ae1bf086e to your computer and use it in GitHub Desktop.
Check if a word is a Palindrome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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