Valid Palindrome on leet code

My First Attempt

/**
 * @param {string} s
 * @return {boolean}
 */
 
var isPalindrome = function(s) {
  let n = s.toLowerCase().replace(/[^a-z]/g, '');
 
  if (n.length % 2 === 0){
    let firstHalf = n.slice(0, n.length / 2)
    let arr = firstHalf.split("")
    arr.reverse()
    let arr2 = arr.join("")
    console.log(arr2)
    if (arr2 === n.slice(n.length/2, n.length)){
      return true
    }
  } else {
    let firstHalf = n.slice(0, (n.length-1) / 2)
    let arr = firstHalf.split("")
    arr.reverse()
    let arr2 = arr.join("")
    if (arr2 === n.slice((n.length - 1)/2 + 1, n.length)){
      return true
    }
  }
 
  return false
};

Very close! Far from eloquent, but for me, eloquence comes easiest with iteration. I was quick to index in on the problem, which was the the description asks for alphanumeric parsing, and I only did the alpha part of that.

Unfortunately, I made a silly mistake, and hastily replaced my a-z to a-z1-9, not even noticing that of course 0 is omitted from that range. So…very quickly fixed it, but then wasted 7 minutes in realizing how my fix was inadequate. I THEN preceded to almost rewrite the second half of the entire algorithm, not even thinking about how this is actually an even case anyways.

Second Attempt

I returned to this really hungry to understand the basics of regex so I went into it here Regex Baby Steps (in Javascript). After playing around a bit, I got this:

function isPalindrome(s: string): boolean {
    const str = s.replace(/[\W_]+/g, '').toLowerCase()
    const backwards = str.split('').reverse().join('')
    return str === backwards
};

Let me go back and see how it compares to the previous iteration.

checks previous code

Oh dear god that’s awful. Cool! making progress!