Palindrome Checker Code

hello, I am a simplest code fan, and I want to share with you my own code of this project to compare it with yours and try to make a code more simple as possible. PS: Excuse me for my english, I am learning english too :wink:

function palindrome(str) {
  // Good luck!
  str = str.toLowerCase()
  let newArr = str.split('')
  let testArr = newArr.filter(val =>{
    return /[a-z]|\d/.test(val)
  })
  let newTest = [...testArr]
  newTest = newTest.join('')
  testArr = testArr.reverse().join('')
  return testArr === newTest
}
1 Like

I wrapped your answer in [spoiler][/spoiler] tags. We don’t usually post answers here. But if you do, please put it in spoiler tags so people not wanting to see the answer have the chance to look away.

1 Like

Hello! Well, my code is not so simple (if simple means short). But for me it seems simple and pure. I know my first mistake is not to use the benefits of Javascript’s functions but anyway that is my code I would like your opinion for it.function palindrome(str) {
var low=str.toLowerCase();
var arr=low.split("");
var len=arr.length;
var arr1=[];
for(var i=0;i<len;i++){
if(/[a-z0-9]/i.test(arr[i])){
arr1.push(arr[i]);
}
}
var len1=arr1.length;
var count=0;
for(var j=0;j<=len1/2;j++)
{
if(arr1[j]==arr1[len1-1-j])
{
count++;
}
}
if(count==j){
return true;
}
return false;
}
palindrome(“eye”);

Impressed – I see the reasoning behind creating a second string, reversing it, and comparing the two, but I like the approach here. I find myself baffled by the calculation to compare the Nth and the length - Nth characters, but my brain is tired.

Well done, both of you. Clearly demonstrating that there’s always more than one way to do a thing. :wink:

2 Likes

I like your code, :wink: it’s more mathemathic than my code… i was never imagine the solution that way… but in the regular expression i think that you don’t need this flag “i” because at the first line you already passed the entry string into lowercase. Anyway, that’s not so important. Greetings :wink:

2 Likes

Oh yes, you are right about that! I probably added the low case later and didn’t notice that. Thank you! I liked your code, too. And you are right that it is important to see others’ code and compare them, it can lead to another way of thinking, when needed.

2 Likes