Tell us what’s happening:
Describe your issue in detail here.
I have one test case that is failing, but when I try testing outside of the freecodecamp test solution, with the same word, I am getting the expected response. I am not able to see an issue and hoping if maybe someone takes a look they can help me identify the issue with the logic
**Your code so far**
function palindrome(str) {
var count = 0;
var testString = str.replaceAll(/[*_#\,.()-:/|]/g, "").replaceAll(" ", '').trim();
var word = testString.toLowerCase();
var arrayLength = word.length;
var i;
for (i = 0; i < arrayLength; i++) {
if (word[i] == word[arrayLength - (i + 1)]) {
count = count + 1;
}
}
if (count == arrayLength) {
return true;
}
else {
return false;
}
}
palindrome("1 eye for of 1 eye.");
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
I do have a return for false which is working for all other test cases but this one.
I actually tested this on a different platform but not running it as a function and was getting the results that the phrase “1 eye for of 1 eye.” is not a palindrome.
Its technically the same logic, not sure why it is failing for this specific phrase. I can’t tell what is wrong with the logic.
But I do see it is returning true using the Onecompiler, so I will try to debug it on there.
Well, maybe the last advice about Python Tutor was not the best… I tried to run your code there and it gives me Type Error… about replaceAll.
My suspicion that Python tutor has some unsupported features for JS.
I think I found some issues.
Here is the test of your regexp
let str = "1 eye for of 1 eye."
let testString = str.replaceAll(/[*_#\,.()-:/|]/g, "").replaceAll(" ", '').trim();
console.log(testString)//eyeforofeye
let str2 = "My age is 0, 0 si ega ym."
let testString2 = str2.replaceAll(/[*_#\,.()-:/|]/g, "").replaceAll(" ", '').trim();
console.log(testString2)//Myageissiegaym
I think your regexp deleting numbers from string, and you need to refactor your regexp.
Also, side note: your defining of TestString is a long and complex line of code, it’s uneasy to analyze it. Maybe add some comments or decompose it for the sake of readability?
The dash has a special meaning in character class, put it as first or last to avoid that special meaning
(The part )-: means all the characters from ) to :)