function palindrome(str) {
let arrSplit = str.split("");
let arrReverse = arrSplit.reverse()
let arrJoin = arrReverse.join("")
let remPunc = /[\W_]/g
let correctedStr = arrJoin.replace(remPunc, '')
let rightOne = correctedStr.toLowerCase();
if (str === rightOne){
return true
}else{
return false;
}
}
palindrome("eye");
In the case of palindrome("_eye")
, str
is _eye
, so when you compare str
to rightone
it is going to return false
. You can not compare to the original string.
ok. so do I have to name a new string and compare with rightOne?
I suggested starting off by replacing the non-alphanumeric characters first and assign the result to a new variable and then perform everything else (starting with the split
method down and then compare that variable to rightOne
. You can then not have to worry about replacing again later in the code like you are doing now.
EDIT: Also make sure you change the new variable I mentioned above to lower case, so it and the reversed string will all be the same case.
and moreover, I used /[\W_]/g to remove all non-alphanumeric characters
Yes you did, but you need to do it at the start with str
since everything else depends on that and the lowercase or uppercase version of it before reversing anything.
ok thank you very much
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.