I’m trying to do the isPalindrome question on LeetCode. I know how to solve this question, but I’m just confused why the following code doesn’t work so that I can better understand the nuances of JS.
I would like to know why JS seems to think that even when reverse and forward.split(“”) are exactly the same, it should still return false.
The issue lies in the if (reverse == forward.split(""))
line. I thought this issue is because JS can’t compare arrays, but a quick online search seems to suggest JS can.
var isPalindrome = function(s) {
let forward = s.replace(/[^0-9a-z]/gi, '').toLowerCase()
let reverse = forward.split("").reverse()
if (reverse == forward.split("")) {
return true
}
return false