Palindromes

I've been doing a lot of networking and professional development and not as much coding this past week so tonight I set myself a lil tech challenge as interview prep. The challenge: write a function that can determine if a given word is a palindrome or not, the output should be the word and true/false ex: elephant, false. To test, I found three palindromes and then added in two that weren't.

My pseudocode to start the process was pretty simple:

// take the word and turn it into an array (split?)
// iterate through the array and check from the outside in
// check if the character at the 0 index === the character at the length -1?
// then check the 1st ==== length -2
// so increment the indices, adding to one and subtracting from the other

And here's the solution!

Although I've only been coding for a few months, it's already been a little while since I used a for loop so I stumbled around for a minute but got this in about 20 minutes. Before using the for loop, I attempted to use find and forEach, which might have worked as well but the for loop gave me more control. I also think it might be possible to solve it without turning the word into an array and instead using charAt() but I didn't play with that for too long.

const determinePalindrome = (word) => {
  const wordAsArray = word.split('')
  var isAPalindrome;

  for(var i = 0; i < wordAsArray.length; i ++ ) {
    if(wordAsArray[i] !== wordAsArray[wordAsArray.length-(i+1)]) {
      isAPalindrome = false
      break
      } else {
      isAPalindrome = true
      }
  }

return {word, isAPalindrome}
}

determinePalindrome('kayak')
determinePalindrome('deified')
determinePalindrome('racecar')
determinePalindrome('elephant')
determinePalindrome('bandit')

Some edge cases I thought of after the fact include palindromes with two matching words in a row, such as peep (which still works in my function) and palindromes that are entire sentences like Red rum, sir, is murder, Step on no pets, and Was it a cat I saw?

These full sentences present quite a few edge cases that my function can't handle such as capitalization, extra spaces and punctuation. Oh well! What's fun about programming is that there is always more room for refactoring, even with a relatively simple logic problem like this!