I am trying to solve an exercise but the code shows errors. Here are the conditions given for the exercise:
Return true if the given string is a palindrome. Otherwise, return false.
- You'll need to remove punctuation and turn everything lower case in order to check for palindromes.
- We'll pass strings with varying formats, such as "racecar", "RaceCar", and "race CAR" among others.
My attempt:
function palindrome(str) {
str = str.toLowerCase();
str = str.replace(",", "");
str = str.replace(".", "");
str = str.replace(":", "");
str = str.replace(";", "");
str = str.replace("-", "");
str = str.replace(",", "");
str = str.replace(" ", "");
for (var i = 0; i <= (str.length / 2); i++) {
if (str[i] != str.length - i) return false;
}
return true;
}