0
function isPalindrome(s) {
    const charArray = Array.from(s.replace(/[^a-zA-Z0-9]/gim,'').toLowerCase());
    const reverseCharArray = Array.from(charArray);
    reverseCharArray.reverse();
    if ( charArray == reverseCharArray ) {
        return true;
    } else {
        return false;
    }
};

let s = "A man, a plan, a canal: Panama"

console.log(isPalindrome(s))

I've tried == too ,but still get the same result.

Is there a better way to tackle this problem? Thank you in advance ;)!

Do I have to create a for loop and check if each index of the array is the same value?

Sporego
  • 376
  • 4
  • 12
  • The reason this is returning false is that the arrays are different - i.e. they contain different objects, even if the objects are equivalent to one another. Use join to reassemble back into a string to check like this: if ( charArray.join() == reverseCharArray.join() ) { – Mark Taylor Aug 04 '20 at 01:16
  • use `const isPalindrome=s=>{s=s.toLowerCase().match(/[a-z0-9]+/g).join('');return (s===[...s].reverse().join(''))}` – Mister Jojo Aug 04 '20 at 01:54

0 Answers0