Goal: create a function
palindrome(str)that returns a normalized string if it is a palindrome; otherwise —null.
/**
* Checks if a string is a palindrome
* A palindrome is a string that reads the same forward and backward.
* The function normalizes the string by removing all non-alphanumeric characters,
* converts to lowercase, then checks if it matches its reversed version.
*
* @param {string} str - Input string to check
* @returns {string|null} Normalized palindrome string or null if not a palindrome
*/
function palindrome(str) {
// Create normalized version of the string:
const cleaned = str
// Convert to lowercase for case-insensitive comparison
.toLowerCase()
// Remove all characters except:
// - Digits (0-9)
// - Latin letters (a-z)
// - Russian letters (а-я, including ё)
// Flags:
// g - global search (entire string)
// i - case insensitive
.replace(/[^0-9a-zа-яё]/gi, '');
// Check if the string is a palindrome:
// 1. Split string into character array (split(''))
// 2. Reverse the array (reverse())
// 3. Join back into string (join(''))
// 4. Compare with original normalized string
return cleaned === cleaned.split('').reverse().join('')
? cleaned // If palindrome - return normalized string
: null; // If not palindrome - return null
}Why this way:
null./**
* Checks if a string is a palindrome using an optimized two-pointer algorithm
* Verifies whether the string reads the same forwards and backwards
* by employing an efficient two-pointer comparison technique.
*
* @param {string} str - Input string to validate
* @returns {string|null} Normalized string (lowercase, alphanumeric only)
* if palindrome, otherwise null
*/
function palindrome(str) {
// 1. Input string normalization:
const cleaned = str
// Convert to lowercase for case-insensitive comparison
.toLowerCase()
// Remove all non-alphanumeric characters:
// - Keeps digits (0-9)
// - Latin letters (a-z)
// - Cyrillic letters (а-яё)
// Flags:
// g - global search (entire string)
// i - case insensitive (though toLowerCase already applied)
.replace(/[^0-9a-zа-яё]/gi, '');
// 2. Two-pointer palindrome verification:
let left = 0; // Left pointer (string start)
let right = cleaned.length - 1; // Right pointer (string end)
// While pointers haven't met in the middle
while (left < right) {
// Compare characters at pointer positions
if (cleaned[left] !== cleaned[right]) {
return null; // Mismatch found - not a palindrome
}
// Move pointers toward center
left++;
right--;
}
// 3. All characters matched - return normalized string
return cleaned;
}Why this way:
Write a function palindrome that returns the normalized string if the input string is a palindrome, and null otherwise. Normalization: lowercase, remove non‑alphanumeric characters.
palindrome('level'); // 'level'
palindrome('RaceCar'); // 'racecar'
palindrome('A man, a plan, a canal: Panama!'); // 'amanaplanacanalpanama'
palindrome('hello'); // nullpalindromenullGoal: create a function
palindrome(str)that returns a normalized string if it is a palindrome; otherwise —null.
/**
* Checks if a string is a palindrome
* A palindrome is a string that reads the same forward and backward.
* The function normalizes the string by removing all non-alphanumeric characters,
* converts to lowercase, then checks if it matches its reversed version.
*
* @param {string} str - Input string to check
* @returns {string|null} Normalized palindrome string or null if not a palindrome
*/
function palindrome(str) {
// Create normalized version of the string:
const cleaned = str
// Convert to lowercase for case-insensitive comparison
.toLowerCase()
// Remove all characters except:
// - Digits (0-9)
// - Latin letters (a-z)
// - Russian letters (а-я, including ё)
// Flags:
// g - global search (entire string)
// i - case insensitive
.replace(/[^0-9a-zа-яё]/gi, '');
// Check if the string is a palindrome:
// 1. Split string into character array (split(''))
// 2. Reverse the array (reverse())
// 3. Join back into string (join(''))
// 4. Compare with original normalized string
return cleaned === cleaned.split('').reverse().join('')
? cleaned // If palindrome - return normalized string
: null; // If not palindrome - return null
}Why this way:
null./**
* Checks if a string is a palindrome using an optimized two-pointer algorithm
* Verifies whether the string reads the same forwards and backwards
* by employing an efficient two-pointer comparison technique.
*
* @param {string} str - Input string to validate
* @returns {string|null} Normalized string (lowercase, alphanumeric only)
* if palindrome, otherwise null
*/
function palindrome(str) {
// 1. Input string normalization:
const cleaned = str
// Convert to lowercase for case-insensitive comparison
.toLowerCase()
// Remove all non-alphanumeric characters:
// - Keeps digits (0-9)
// - Latin letters (a-z)
// - Cyrillic letters (а-яё)
// Flags:
// g - global search (entire string)
// i - case insensitive (though toLowerCase already applied)
.replace(/[^0-9a-zа-яё]/gi, '');
// 2. Two-pointer palindrome verification:
let left = 0; // Left pointer (string start)
let right = cleaned.length - 1; // Right pointer (string end)
// While pointers haven't met in the middle
while (left < right) {
// Compare characters at pointer positions
if (cleaned[left] !== cleaned[right]) {
return null; // Mismatch found - not a palindrome
}
// Move pointers toward center
left++;
right--;
}
// 3. All characters matched - return normalized string
return cleaned;
}Why this way:
Write a function palindrome that returns the normalized string if the input string is a palindrome, and null otherwise. Normalization: lowercase, remove non‑alphanumeric characters.
palindrome('level'); // 'level'
palindrome('RaceCar'); // 'racecar'
palindrome('A man, a plan, a canal: Panama!'); // 'amanaplanacanalpanama'
palindrome('hello'); // nullpalindromenullThe code editor is intentionally hidden on mobile.
Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.
📖 Now: Study the task, think through the solution. Act like a strategist.
💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!