Palindrome
Write isPalindrome(text). First settle the rules: do we ignore case, spaces and punctuation? In this task we compare all characters and ignore case only.
Following the task's unusual contract, we return true or 0; a normal API would be better off returning a consistent boolean. Compare code points, not UTF-16 units — emoji are characters too.
You will practise
- Settle the comparison rules
- Compare characters from both ends
- Handle characters outside the BMP (emoji)
Examples
- isPalindrome('Kayak')→ true
- isPalindrome('React')→ 0
The 4 tests
Tests written from the card: the Kayak/React examples, comparing all characters except case, and an array of code points.
the task's examples
assert.equal(isPalindrome('Kayak'), true);
assert.equal(isPalindrome('React'), 0);ignores case only
assert.equal(isPalindrome('AbBa'), true);
assert.equal(isPalindrome('kajak '), 0, 'a space is compared like any other character');empty text and a single character
assert.equal(isPalindrome(''), true);
assert.equal(isPalindrome('x'), true);compares code points (emoji)
assert.equal(isPalindrome('a😀a'), true);
assert.equal(isPalindrome('😀b'), 0);Related questions
Source: Full Stack / JS — task card — The task and reference solution come from the card; the tests were written from its description.