const isPalindrome = (s) => { // make sure all lowercase and remove non-alphanumeric characters s = s.toLowerCase().replace(/[^a-z]/g, ''); // initialize two pointers, one at beginning and one at the end let i = 0, j = s.length - 1; // Move pointers towards each other until they meet in the middle while (i < j) { // return false is char dont match ==> not a palindrome if (s[i] !== s[j]) { return false; } i++; // Move left pointer to the right j--; // Move right pointer to the left } // We reached the middle of the string without finding a mismatch, so it is a palindrome. return true; }; const str1 = 'Racecar'; const str2 = 'Hello, world!'; console.log(isPalindrome(str1)); // true console.log(isPalindrome(str2)); // false