Drill

ProblemsTypeScript › warmup

Palindrome check

easywarmupTypeScript

A puzzle feature checks whether a phrase reads the same backwards.

isPalindrome(text: string) → bool

Solve it in the editor →

Where you start

function isPalindrome(text: string): boolean {
  
}

Worked examples

CallResult
isPalindrome("")true
isPalindrome("racecar")true
isPalindrome("A man, a plan, a canal: Panama")true
isPalindrome("hello")false

Hint

Walk one index from each end, skipping anything that is not alphanumeric.

Reference solution in TypeScript
function isPalindrome(text: string): boolean {
  const s = text.toLowerCase().replace(/[^a-z0-9]/g, '');
  for (let i = 0, j = s.length - 1; i < j; i++, j--) {
    if (s[i] !== s[j]) return false;
  }
  return true;
}

The same problem in another language

More warmup problems in TypeScript