# How to make my solution more sophisticated?

How to make my solution more sophisticated?
0

#1

Hi, everyone! Today I finished the fourth challenge of Basic Algorithms block - Check for Palindromes.
Although every test is positive, I feel like my solution is too bulky. I would like to figure our, how can I make it shorter but as functional as it is. Maybe there is some other algorithm that can deal with the problem in more efficient way.

P.S. Thank you, freeCodeCamp for this initiative and the knowledge you give us.

function palindrome(str) {
var result,
newStr,
regExp = /[A-Za-z0-9]/,
arr = str.toLowerCase().split("");

arr = arr.filter(function(value){
return regExp.test(value);
});
newStr = arr.join("");
result = arr.reverse().join("").toLowerCase();

return newStr === result;
}

#2

Don’t spend too much of your time on trying to make your code shorter, it’ll come with experience and by reading other’s code (I recommend you to join codewars for that).

But if you really like to see shorter version of your code, here it is:

function palindrome(str) {
str = str.toLowerCase().replace(/[^\da-z]/g, '');
return str.split('').reverse().join('') === str;
}

First you clean up the input string, and then you compare reversed string with the cleaned input string.

#3

Thanks a lot, seems much better.

I’m actually joined CodeWars last year, have 6th kuy there