The challenge
Write isPalindrome(text) that returns true if the text reads the same forwards and backwards, ignoring case, spaces and punctuation.
isPalindrome("Racecar") // true
isPalindrome("Was it a car or a cat I saw?") // true
isPalindrome("hello") // false
Try it
Write your answer below, then press Check my answer. Your code runs right here in your browser, and the tests tell you what works.
It prints
What the tests check
isPalindrome("Racecar")givestrueisPalindrome("Was it a car or a cat I saw?")givestrueisPalindrome("hello")givesfalseisPalindrome("a")givestrue
Hints
Stuck? Open one hint at a time. Try again after each one.
Hint 1
First clean the text: lowercase it and keep only the letters a to z.
Hint 2
Then compare it with its reverse: clean.split("").reverse().join("").
A solution
There’s always more than one right way. Read it, make sure you understand it, then try writing it yourself without looking.
Show one possible solution
function isPalindrome(text) {
let clean = "";
for (const c of text.toLowerCase()) {
if (c >= "a" && c <= "z") clean += c;
}
return clean === clean.split("").reverse().join("");
}
Learn what you need
This exercise uses what Grade 7: Arrays: Lists of Things teaches. Read those notes first if any of it is new.