JavaScript Algorithms and Data Structures Projects: Palindrome Checker

I tried to solve this exercice with 2 diferent ways and I still can’t pass all the tests. Can someone tell me what i’m doing wrong?

Here’s my first attempt:

function Palindrome(str) {
  let array = str.toLowerCase().split(/\W_/g);
  for(let i = 0; i <array.length; i++) {
     for(let j= array.length-1; j>=0; j--) {
       if(i!==j) {
          return false;
        }
       return true;
    }
 }
}

And here’s my second attempt;

function palindrome(str) {
  let char = /[\W_]/g;
  let lowerC = str.toLowerCase().replace(char, '');
  let rev = lowerC.split('').reverse().join(''); 
  return rev === lowerC;
}

Try refreshing your browser, and/or clearing the cache, the second one works fine. You haven’t quite got the logic right in the first one, I would use replace to get rid of the stuff you don’t want (like you do with the second one).

1 Like

Thank you! I cleared my cache and it works now

2 Likes